Skip to content

Commit a0acb26

Browse files
committed
Code style tweaks and increasing test coverage
1 parent f0b9105 commit a0acb26

File tree

10 files changed

+125
-67
lines changed

10 files changed

+125
-67
lines changed

src/Jenssegers/Mongodb/Auth/DatabaseReminderRepository.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,17 @@ protected function getPayload($email, $token)
2525
*/
2626
protected function reminderExpired($reminder)
2727
{
28-
// Convert to array so that we can pass it to the parent method
29-
if (is_object($reminder))
30-
{
31-
$reminder = (array) $reminder;
32-
}
33-
3428
// Convert MongoDate to a date string.
3529
if ($reminder['created_at'] instanceof MongoDate)
3630
{
37-
$date = new DateTime();
31+
$date = new DateTime;
32+
3833
$date->setTimestamp($reminder['created_at']->sec);
3934

4035
$reminder['created_at'] = $date->format('Y-m-d H:i:s');
4136
}
4237

43-
// DEPRECATED: Convert DateTime to a date string.
38+
// Convert DateTime to a date string (backwards compatibility).
4439
elseif (is_array($reminder['created_at']))
4540
{
4641
$date = DateTime::__set_state($reminder['created_at']);

src/Jenssegers/Mongodb/Collection.php

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class Collection {
2626
public function __construct(Connection $connection, MongoCollection $collection)
2727
{
2828
$this->connection = $connection;
29+
2930
$this->collection = $collection;
3031
}
3132

@@ -38,29 +39,34 @@ public function __construct(Connection $connection, MongoCollection $collection)
3839
*/
3940
public function __call($method, $parameters)
4041
{
42+
$query = array();
43+
4144
// Build the query string.
42-
$query = $parameters;
43-
foreach ($query as &$param)
45+
foreach ($parameters as $parameter)
4446
{
4547
try
4648
{
47-
$param = json_encode($param);
49+
$query[] = json_encode($parameter);
4850
}
4951
catch (Exception $e)
5052
{
51-
$param = '{...}';
53+
$query[] = '{...}';
5254
}
5355
}
5456

5557
$start = microtime(true);
5658

57-
// Execute the query.
5859
$result = call_user_func_array(array($this->collection, $method), $parameters);
5960

60-
// Log the query.
61-
$this->connection->logQuery(
62-
$this->collection->getName() . '.' . $method . '(' . join(',', $query) . ')',
63-
array(), $this->connection->getElapsedTime($start));
61+
// Once we have run the query we will calculate the time that it took to run and
62+
// then log the query, bindings, and execution time so we will report them on
63+
// the event that the developer needs them. We'll log time in milliseconds.
64+
$time = $this->connection->getElapsedTime($start);
65+
66+
// Convert the query to a readable string.
67+
$queryString = $this->collection->getName() . '.' . $method . '(' . join(',', $query) . ')';
68+
69+
$this->connection->logQuery($queryString, array(), $time);
6470

6571
return $result;
6672
}

src/Jenssegers/Mongodb/Eloquent/Builder.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ public function increment($column, $amount = 1, array $extra = array())
116116
// sync the original attributes. We need to change the attribute
117117
// temporary in order to trigger an update query.
118118
$this->model->{$column} = null;
119+
119120
$this->model->syncOriginalAttribute($column);
120121

121122
$result = $this->model->update(array($column => $value));
@@ -146,6 +147,7 @@ public function decrement($column, $amount = 1, array $extra = array())
146147
// sync the original attributes. We need to change the attribute
147148
// temporary in order to trigger an update query.
148149
$this->model->{$column} = null;
150+
149151
$this->model->syncOriginalAttribute($column);
150152

151153
return $this->model->update(array($column => $value));

src/Jenssegers/Mongodb/Eloquent/Collection.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php namespace Jenssegers\Mongodb\Eloquent;
22

3-
use Illuminate\Database\Eloquent\Collection as BaseCollection;
3+
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
44

5-
class Collection extends BaseCollection {
5+
class Collection extends EloquentCollection {
66

77
/**
88
* Simulate a get clause on the collection.
@@ -63,6 +63,10 @@ public function where($key, $operator = null, $value = null)
6363
return $actual >= $value;
6464
break;
6565

66+
case '<=':
67+
return $actual <= $value;
68+
break;
69+
6670
case 'between':
6771
return $actual >= $value[0] and $actual <= $value[1];
6872
break;

src/Jenssegers/Mongodb/Model.php

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -269,12 +269,8 @@ public function getAttribute($key)
269269
*/
270270
protected function getAttributeFromArray($key)
271271
{
272-
if (array_key_exists($key, $this->attributes))
273-
{
274-
return $this->attributes[$key];
275-
}
276-
277-
elseif (str_contains($key, '.'))
272+
// Support keys in dot notation.
273+
if (str_contains($key, '.'))
278274
{
279275
$attributes = array_dot($this->attributes);
280276

@@ -283,6 +279,8 @@ protected function getAttributeFromArray($key)
283279
return $attributes[$key];
284280
}
285281
}
282+
283+
return parent::getAttributeFromArray($key);
286284
}
287285

288286
/**
@@ -380,10 +378,8 @@ public function push()
380378
list($column, $values) = $parameters;
381379
}
382380

383-
if ( ! is_array($values))
384-
{
385-
$values = array($values);
386-
}
381+
// Do batch push by default.
382+
if ( ! is_array($values)) $values = array($values);
387383

388384
$query = $this->setKeysForSaveQuery($this->newQuery());
389385

@@ -402,10 +398,8 @@ public function push()
402398
*/
403399
public function pull($column, $values)
404400
{
405-
if ( ! is_array($values))
406-
{
407-
$values = array($values);
408-
}
401+
// Do batch pull by default.
402+
if ( ! is_array($values)) $values = array($values);
409403

410404
$query = $this->setKeysForSaveQuery($this->newQuery());
411405

src/Jenssegers/Mongodb/Query/Builder.php

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66
use DateTime;
77
use Closure;
88

9+
use Illuminate\Database\Query\Builder as QueryBuilder;
910
use Illuminate\Database\Query\Expression;
1011
use Jenssegers\Mongodb\Connection;
1112

12-
class Builder extends \Illuminate\Database\Query\Builder {
13+
class Builder extends QueryBuilder {
1314

1415
/**
1516
* The database collection
@@ -193,6 +194,7 @@ public function getFresh($columns = array())
193194
foreach ($this->columns as $column)
194195
{
195196
$key = str_replace('.', '_', $column);
197+
196198
$group[$key] = array('$last' => '$' . $column);
197199
}
198200
}
@@ -374,17 +376,18 @@ public function insert(array $values)
374376
// Since every insert gets treated like a batch insert, we will have to detect
375377
// if the user is inserting a single document or an array of documents.
376378
$batch = true;
379+
377380
foreach ($values as $value)
378381
{
379382
// As soon as we find a value that is not an array we assume the user is
380383
// inserting a single document.
381-
if (!is_array($value))
384+
if ( ! is_array($value))
382385
{
383386
$batch = false; break;
384387
}
385388
}
386389

387-
if (!$batch) $values = array($values);
390+
if ( ! $batch) $values = array($values);
388391

389392
// Batch insert
390393
$result = $this->collection->batchInsert($values);
@@ -405,7 +408,7 @@ public function insertGetId(array $values, $sequence = null)
405408

406409
if (1 == (int) $result['ok'])
407410
{
408-
if (!$sequence)
411+
if (is_null($sequence))
409412
{
410413
$sequence = '_id';
411414
}
@@ -454,6 +457,7 @@ public function increment($column, $amount = 1, array $extra = array(), array $o
454457
$this->where(function($query) use ($column)
455458
{
456459
$query->where($column, 'exists', false);
460+
457461
$query->orWhereNotNull($column);
458462
});
459463

@@ -502,6 +506,7 @@ public function pluck($column)
502506
public function delete($id = null)
503507
{
504508
$wheres = $this->compileWheres();
509+
505510
$result = $this->collection->remove($wheres);
506511

507512
if (1 == (int) $result['ok'])
@@ -577,13 +582,13 @@ public function push($column, $value = null, $unique = false)
577582
$operator = $unique ? '$addToSet' : '$push';
578583

579584
// Check if we are pushing multiple values.
580-
$multipleValues = (is_array($value) and array_keys($value) === range(0, count($value) - 1));
585+
$batch = (is_array($value) and array_keys($value) === range(0, count($value) - 1));
581586

582587
if (is_array($column))
583588
{
584589
$query = array($operator => $column);
585590
}
586-
else if ($multipleValues)
591+
else if ($batch)
587592
{
588593
$query = array($operator => array($column => array('$each' => $value)));
589594
}
@@ -605,10 +610,10 @@ public function push($column, $value = null, $unique = false)
605610
public function pull($column, $value = null)
606611
{
607612
// Check if we passed an associative array.
608-
$multipleValues = (is_array($value) and array_keys($value) === range(0, count($value) - 1));
613+
$batch = (is_array($value) and array_keys($value) === range(0, count($value) - 1));
609614

610615
// If we are pulling multiple values, we need to use $pullAll.
611-
$operator = $multipleValues ? '$pullAll' : '$pull';
616+
$operator = $batch ? '$pullAll' : '$pull';
612617

613618
if (is_array($column))
614619
{

src/Jenssegers/Mongodb/Relations/EmbedsOne.php

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -131,21 +131,4 @@ public function delete()
131131
return $this->performDelete($model);
132132
}
133133

134-
/**
135-
* Check if a model is already embedded.
136-
*
137-
* @param mixed $key
138-
* @return bool
139-
*/
140-
public function contains($key)
141-
{
142-
if ($key instanceof Model) $key = $key->getKey();
143-
144-
$embedded = $this->getEmbedded();
145-
146-
$primaryKey = $this->related->getKeyName();
147-
148-
return ($embedded and $embedded[$primaryKey] == $key);
149-
}
150-
151134
}

src/Jenssegers/Mongodb/Schema/Blueprint.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class Blueprint extends \Illuminate\Database\Schema\Blueprint {
3636
public function __construct(Connection $connection, $collection)
3737
{
3838
$this->connection = $connection;
39+
3940
$this->collection = $connection->getCollection($collection);
4041
}
4142

@@ -50,11 +51,12 @@ public function index($columns = null, $options = array())
5051
{
5152
$columns = $this->fluent($columns);
5253

53-
// Columns are passed as a default array
54+
// Columns are passed as a default array.
5455
if (is_array($columns) && is_int(key($columns)))
5556
{
56-
// Transform the columns to the required array format
57+
// Transform the columns to the required array format.
5758
$transform = array();
59+
5860
foreach ($columns as $column)
5961
{
6062
$transform[$column] = 1;
@@ -78,11 +80,12 @@ public function dropIndex($columns = null)
7880
{
7981
$columns = $this->fluent($columns);
8082

81-
// Columns are passed as a default array
83+
// Columns are passed as a default array.
8284
if (is_array($columns) && is_int(key($columns)))
8385
{
84-
// Transform the columns to the required array format
86+
// Transform the columns to the required array format.
8587
$transform = array();
88+
8689
foreach ($columns as $column)
8790
{
8891
$transform[$column] = 1;
@@ -105,6 +108,7 @@ public function dropIndex($columns = null)
105108
public function unique($columns = null, $name = null)
106109
{
107110
$columns = $this->fluent($columns);
111+
108112
$this->index($columns, array('unique' => true));
109113

110114
return $this;
@@ -119,6 +123,7 @@ public function unique($columns = null, $name = null)
119123
public function background($columns = null)
120124
{
121125
$columns = $this->fluent($columns);
126+
122127
$this->index($columns, array('background' => true));
123128

124129
return $this;
@@ -149,6 +154,7 @@ public function sparse($columns = null)
149154
public function expire($columns, $seconds)
150155
{
151156
$columns = $this->fluent($columns);
157+
152158
$this->index($columns, array('expireAfterSeconds' => $seconds));
153159

154160
return $this;
@@ -163,8 +169,9 @@ public function create()
163169
{
164170
$collection = $this->collection->getName();
165171

166-
// Ensure the collection is created
167172
$db = $this->connection->getMongoDB();
173+
174+
// Ensure the collection is created.
168175
$db->createCollection($collection);
169176
}
170177

@@ -189,6 +196,7 @@ public function drop()
189196
protected function addColumn($type, $name, array $parameters = array())
190197
{
191198
$this->fluent($name);
199+
192200
return $this;
193201
}
194202

@@ -221,6 +229,7 @@ protected function fluent($columns = null)
221229
*/
222230
public function __call($method, $args)
223231
{
232+
// Dummy.
224233
return $this;
225234
}
226235

0 commit comments

Comments
 (0)