Completed
Branch master (901ac8)
by Rémi
11:17
created
src/System/Wrappers/ObjectWrapper.php 2 patches
Doc Comments   -1 removed lines patch added patch discarded remove patch
@@ -35,7 +35,6 @@
 block discarded – undo
35 35
     /**
36 36
      * Object Wrapper constructor.
37 37
      *
38
-     * @param mixed                  $object
39 38
      * @param Analogue\ORM\EntityMap $entityMap
40 39
      *
41 40
      * @return void
Please login to merge, or discard this patch.
Indentation   +221 added lines, -221 removed lines patch added patch discarded remove patch
@@ -10,225 +10,225 @@
 block discarded – undo
10 10
  */
11 11
 class ObjectWrapper extends Wrapper
12 12
 {
13
-    /**
14
-     * Internal Representation of analogue's entity attributes.
15
-     *
16
-     * @var array
17
-     */
18
-    protected $attributes = [];
19
-
20
-    /**
21
-     * Object properties that are not a part of the entity attributes,
22
-     * but which are needed to correctly hydrate the Object.
23
-     *
24
-     * @var array
25
-     */
26
-    protected $unmanagedProperties = [];
27
-
28
-    /**
29
-     * The hydrator for the wrapped object.
30
-     *
31
-     * @var HydratorInterface
32
-     */
33
-    protected $hydrator;
34
-
35
-    /**
36
-     * Object Wrapper constructor.
37
-     *
38
-     * @param mixed                  $object
39
-     * @param Analogue\ORM\EntityMap $entityMap
40
-     *
41
-     * @return void
42
-     */
43
-    public function __construct($entity, $entityMap, HydratorInterface $hydrator)
44
-    {
45
-        $this->hydrator = $hydrator;
46
-        parent::__construct($entity, $entityMap);
47
-        $this->attributes = $this->dehydrate($entity);
48
-    }
49
-
50
-    /**
51
-     * Returns the wrapped entity.
52
-     *
53
-     * @return mixed
54
-     */
55
-    public function getObject()
56
-    {
57
-        $this->hydrate();
58
-
59
-        return $this->entity;
60
-    }
61
-
62
-    /**
63
-     * Extract entity attributes / properties to an array of attributes.
64
-     *
65
-     * @param mixed $entity
66
-     *
67
-     * @return array
68
-     */
69
-    protected function dehydrate($entity) : array
70
-    {
71
-        $properties = $this->hydrator->extract($entity);
72
-
73
-        $this->unmanagedProperties = array_except($properties, $this->getManagedProperties());
74
-
75
-        return $this->attributesFromProperties($properties);
76
-    }
77
-
78
-    /**
79
-     * Hydrate object's properties/attribute from the internal array representation.
80
-     *
81
-     * @return mixed
82
-     */
83
-    public function hydrate()
84
-    {
85
-        $properties = $this->propertiesFromAttributes($this->attributes) + $this->unmanagedProperties;
86
-        $this->hydrator->hydrate($properties, $this->entity);
87
-    }
88
-
89
-    /**
90
-     * Return properties that will be extracted from the entity.
91
-     *
92
-     * @return array
93
-     */
94
-    protected function getManagedProperties() : array
95
-    {
96
-        $properties = $this->entityMap->getProperties();
97
-
98
-        $attributesName = $this->entityMap->getAttributesArrayName();
99
-
100
-        return $attributesName == null ? $properties : array_merge($properties, [$attributesName]);
101
-    }
102
-
103
-    /**
104
-     * Convert object's properties to analogue's internal attributes representation.
105
-     *
106
-     * @param array $properties
107
-     *
108
-     * @return array
109
-     */
110
-    protected function attributesFromProperties(array $properties) : array
111
-    {
112
-        // First, we'll only keep the entities that are part of the Entity's
113
-        // attributes
114
-        $managedProperties = $this->getManagedProperties();
115
-
116
-        $properties = array_only($properties, $managedProperties);
117
-
118
-        // If the entity does not uses the attributes array to store
119
-        // part of its attributes, we'll directly return the properties
120
-        if (!$this->entityMap->usesAttributesArray()) {
121
-            return $properties;
122
-        }
123
-
124
-        $arrayName = $this->entityMap->getAttributesArrayName();
125
-
126
-        if (!array_key_exists($arrayName, $properties)) {
127
-            throw new MappingException("Property $arrayName not set on object of type ".$this->getEntityClass());
128
-        }
129
-
130
-        if (!is_array($properties[$arrayName])) {
131
-            throw new MappingException("Property $arrayName should be an array.");
132
-        }
133
-
134
-        $attributes = $properties[$arrayName];
135
-
136
-        unset($properties[$arrayName]);
137
-
138
-        return $properties + $attributes;
139
-    }
140
-
141
-    /**
142
-     * Convert internal representation of attributes to an array of properties
143
-     * that can hydrate the actual object.
144
-     *
145
-     * @param array $attributes
146
-     *
147
-     * @return array
148
-     */
149
-    protected function propertiesFromAttributes(array $attributes) : array
150
-    {
151
-        $attributes = $this->attributes;
152
-
153
-        // Get all managed properties
154
-        $propertyNames = $this->entityMap->getProperties();
155
-
156
-        $propertyAttributes = array_only($attributes, $propertyNames);
157
-        $attributesArray = array_except($attributes, $propertyNames);
158
-
159
-        $attributesArrayName = $this->entityMap->getAttributesArrayName();
160
-
161
-        if ($attributesArrayName) {
162
-            $propertyAttributes[$attributesArrayName] = $attributesArray;
163
-        }
164
-
165
-        return $propertyAttributes;
166
-    }
167
-
168
-    /**
169
-     * Method used by the mapper to set the object
170
-     * attribute raw values (hydration).
171
-     *
172
-     * @param array $attributes
173
-     *
174
-     * @return void
175
-     */
176
-    public function setEntityAttributes(array $attributes)
177
-    {
178
-        $this->attributes = $attributes;
179
-    }
180
-
181
-    /**
182
-     * Method used by the mapper to get the
183
-     * raw object's values.
184
-     *
185
-     * @return array
186
-     */
187
-    public function getEntityAttributes() : array
188
-    {
189
-        return $this->attributes;
190
-    }
191
-
192
-    /**
193
-     * Method used by the mapper to set raw
194
-     * key-value pair.
195
-     *
196
-     * @param string $key
197
-     * @param string $value
198
-     *
199
-     * @return void
200
-     */
201
-    public function setEntityAttribute($key, $value)
202
-    {
203
-        $this->attributes[$key] = $value;
204
-    }
205
-
206
-    /**
207
-     * Method used by the mapper to get single
208
-     * key-value pair.
209
-     *
210
-     * @param string $key
211
-     *
212
-     * @return mixed|null
213
-     */
214
-    public function getEntityAttribute($key)
215
-    {
216
-        if ($this->hasAttribute($key)) {
217
-            return $this->attributes[$key];
218
-        } else {
219
-            return;
220
-        }
221
-    }
222
-
223
-    /**
224
-     * Test if a given attribute exists.
225
-     *
226
-     * @param string $key
227
-     *
228
-     * @return bool
229
-     */
230
-    public function hasAttribute($key) : bool
231
-    {
232
-        return array_key_exists($key, $this->attributes) ? true : false;
233
-    }
13
+	/**
14
+	 * Internal Representation of analogue's entity attributes.
15
+	 *
16
+	 * @var array
17
+	 */
18
+	protected $attributes = [];
19
+
20
+	/**
21
+	 * Object properties that are not a part of the entity attributes,
22
+	 * but which are needed to correctly hydrate the Object.
23
+	 *
24
+	 * @var array
25
+	 */
26
+	protected $unmanagedProperties = [];
27
+
28
+	/**
29
+	 * The hydrator for the wrapped object.
30
+	 *
31
+	 * @var HydratorInterface
32
+	 */
33
+	protected $hydrator;
34
+
35
+	/**
36
+	 * Object Wrapper constructor.
37
+	 *
38
+	 * @param mixed                  $object
39
+	 * @param Analogue\ORM\EntityMap $entityMap
40
+	 *
41
+	 * @return void
42
+	 */
43
+	public function __construct($entity, $entityMap, HydratorInterface $hydrator)
44
+	{
45
+		$this->hydrator = $hydrator;
46
+		parent::__construct($entity, $entityMap);
47
+		$this->attributes = $this->dehydrate($entity);
48
+	}
49
+
50
+	/**
51
+	 * Returns the wrapped entity.
52
+	 *
53
+	 * @return mixed
54
+	 */
55
+	public function getObject()
56
+	{
57
+		$this->hydrate();
58
+
59
+		return $this->entity;
60
+	}
61
+
62
+	/**
63
+	 * Extract entity attributes / properties to an array of attributes.
64
+	 *
65
+	 * @param mixed $entity
66
+	 *
67
+	 * @return array
68
+	 */
69
+	protected function dehydrate($entity) : array
70
+	{
71
+		$properties = $this->hydrator->extract($entity);
72
+
73
+		$this->unmanagedProperties = array_except($properties, $this->getManagedProperties());
74
+
75
+		return $this->attributesFromProperties($properties);
76
+	}
77
+
78
+	/**
79
+	 * Hydrate object's properties/attribute from the internal array representation.
80
+	 *
81
+	 * @return mixed
82
+	 */
83
+	public function hydrate()
84
+	{
85
+		$properties = $this->propertiesFromAttributes($this->attributes) + $this->unmanagedProperties;
86
+		$this->hydrator->hydrate($properties, $this->entity);
87
+	}
88
+
89
+	/**
90
+	 * Return properties that will be extracted from the entity.
91
+	 *
92
+	 * @return array
93
+	 */
94
+	protected function getManagedProperties() : array
95
+	{
96
+		$properties = $this->entityMap->getProperties();
97
+
98
+		$attributesName = $this->entityMap->getAttributesArrayName();
99
+
100
+		return $attributesName == null ? $properties : array_merge($properties, [$attributesName]);
101
+	}
102
+
103
+	/**
104
+	 * Convert object's properties to analogue's internal attributes representation.
105
+	 *
106
+	 * @param array $properties
107
+	 *
108
+	 * @return array
109
+	 */
110
+	protected function attributesFromProperties(array $properties) : array
111
+	{
112
+		// First, we'll only keep the entities that are part of the Entity's
113
+		// attributes
114
+		$managedProperties = $this->getManagedProperties();
115
+
116
+		$properties = array_only($properties, $managedProperties);
117
+
118
+		// If the entity does not uses the attributes array to store
119
+		// part of its attributes, we'll directly return the properties
120
+		if (!$this->entityMap->usesAttributesArray()) {
121
+			return $properties;
122
+		}
123
+
124
+		$arrayName = $this->entityMap->getAttributesArrayName();
125
+
126
+		if (!array_key_exists($arrayName, $properties)) {
127
+			throw new MappingException("Property $arrayName not set on object of type ".$this->getEntityClass());
128
+		}
129
+
130
+		if (!is_array($properties[$arrayName])) {
131
+			throw new MappingException("Property $arrayName should be an array.");
132
+		}
133
+
134
+		$attributes = $properties[$arrayName];
135
+
136
+		unset($properties[$arrayName]);
137
+
138
+		return $properties + $attributes;
139
+	}
140
+
141
+	/**
142
+	 * Convert internal representation of attributes to an array of properties
143
+	 * that can hydrate the actual object.
144
+	 *
145
+	 * @param array $attributes
146
+	 *
147
+	 * @return array
148
+	 */
149
+	protected function propertiesFromAttributes(array $attributes) : array
150
+	{
151
+		$attributes = $this->attributes;
152
+
153
+		// Get all managed properties
154
+		$propertyNames = $this->entityMap->getProperties();
155
+
156
+		$propertyAttributes = array_only($attributes, $propertyNames);
157
+		$attributesArray = array_except($attributes, $propertyNames);
158
+
159
+		$attributesArrayName = $this->entityMap->getAttributesArrayName();
160
+
161
+		if ($attributesArrayName) {
162
+			$propertyAttributes[$attributesArrayName] = $attributesArray;
163
+		}
164
+
165
+		return $propertyAttributes;
166
+	}
167
+
168
+	/**
169
+	 * Method used by the mapper to set the object
170
+	 * attribute raw values (hydration).
171
+	 *
172
+	 * @param array $attributes
173
+	 *
174
+	 * @return void
175
+	 */
176
+	public function setEntityAttributes(array $attributes)
177
+	{
178
+		$this->attributes = $attributes;
179
+	}
180
+
181
+	/**
182
+	 * Method used by the mapper to get the
183
+	 * raw object's values.
184
+	 *
185
+	 * @return array
186
+	 */
187
+	public function getEntityAttributes() : array
188
+	{
189
+		return $this->attributes;
190
+	}
191
+
192
+	/**
193
+	 * Method used by the mapper to set raw
194
+	 * key-value pair.
195
+	 *
196
+	 * @param string $key
197
+	 * @param string $value
198
+	 *
199
+	 * @return void
200
+	 */
201
+	public function setEntityAttribute($key, $value)
202
+	{
203
+		$this->attributes[$key] = $value;
204
+	}
205
+
206
+	/**
207
+	 * Method used by the mapper to get single
208
+	 * key-value pair.
209
+	 *
210
+	 * @param string $key
211
+	 *
212
+	 * @return mixed|null
213
+	 */
214
+	public function getEntityAttribute($key)
215
+	{
216
+		if ($this->hasAttribute($key)) {
217
+			return $this->attributes[$key];
218
+		} else {
219
+			return;
220
+		}
221
+	}
222
+
223
+	/**
224
+	 * Test if a given attribute exists.
225
+	 *
226
+	 * @param string $key
227
+	 *
228
+	 * @return bool
229
+	 */
230
+	public function hasAttribute($key) : bool
231
+	{
232
+		return array_key_exists($key, $this->attributes) ? true : false;
233
+	}
234 234
 }
Please login to merge, or discard this patch.
src/MagicSetters.php 1 patch
Indentation   +23 added lines, -23 removed lines patch added patch discarded remove patch
@@ -7,28 +7,28 @@
 block discarded – undo
7 7
  */
8 8
 trait MagicSetters
9 9
 {
10
-    /**
11
-     * Dynamically set attributes on the entity.
12
-     *
13
-     * @param string $key
14
-     * @param mixed  $value
15
-     *
16
-     * @return void
17
-     */
18
-    public function __set($key, $value)
19
-    {
20
-        $this->attributes[$key] = $value;
21
-    }
10
+	/**
11
+	 * Dynamically set attributes on the entity.
12
+	 *
13
+	 * @param string $key
14
+	 * @param mixed  $value
15
+	 *
16
+	 * @return void
17
+	 */
18
+	public function __set($key, $value)
19
+	{
20
+		$this->attributes[$key] = $value;
21
+	}
22 22
 
23
-    /**
24
-     * Unset an attribute on the entity.
25
-     *
26
-     * @param string $key
27
-     *
28
-     * @return void
29
-     */
30
-    public function __unset($key)
31
-    {
32
-        unset($this->attributes[$key]);
33
-    }
23
+	/**
24
+	 * Unset an attribute on the entity.
25
+	 *
26
+	 * @param string $key
27
+	 *
28
+	 * @return void
29
+	 */
30
+	public function __unset($key)
31
+	{
32
+		unset($this->attributes[$key]);
33
+	}
34 34
 }
Please login to merge, or discard this patch.
src/Commands/Command.php 1 patch
Indentation   +24 added lines, -24 removed lines patch added patch discarded remove patch
@@ -7,32 +7,32 @@
 block discarded – undo
7 7
 
8 8
 abstract class Command
9 9
 {
10
-    /**
11
-     * The aggregated entity on which the command is executed.
12
-     *
13
-     * @var \Analogue\ORM\System\Aggregate
14
-     */
15
-    protected $aggregate;
10
+	/**
11
+	 * The aggregated entity on which the command is executed.
12
+	 *
13
+	 * @var \Analogue\ORM\System\Aggregate
14
+	 */
15
+	protected $aggregate;
16 16
 
17
-    /**
18
-     * Query Builder instance.
19
-     *
20
-     * @var \Illuminate\Database\Query\Builder
21
-     */
22
-    protected $query;
17
+	/**
18
+	 * Query Builder instance.
19
+	 *
20
+	 * @var \Illuminate\Database\Query\Builder
21
+	 */
22
+	protected $query;
23 23
 
24
-    /**
25
-     * Command constructor.
26
-     *
27
-     * @param Aggregate                                                 $aggregate
28
-     * @param QueryAdapter|\Analogue\ORM\Drivers\IlluminateQueryAdapter $query
29
-     */
30
-    public function __construct(Aggregate $aggregate, Builder $query)
31
-    {
32
-        $this->aggregate = $aggregate;
24
+	/**
25
+	 * Command constructor.
26
+	 *
27
+	 * @param Aggregate                                                 $aggregate
28
+	 * @param QueryAdapter|\Analogue\ORM\Drivers\IlluminateQueryAdapter $query
29
+	 */
30
+	public function __construct(Aggregate $aggregate, Builder $query)
31
+	{
32
+		$this->aggregate = $aggregate;
33 33
 
34
-        $this->query = $query->from($aggregate->getEntityMap()->getTable());
35
-    }
34
+		$this->query = $query->from($aggregate->getEntityMap()->getTable());
35
+	}
36 36
 
37
-    abstract public function execute();
37
+	abstract public function execute();
38 38
 }
Please login to merge, or discard this patch.
src/Commands/Delete.php 1 patch
Indentation   +26 added lines, -26 removed lines patch added patch discarded remove patch
@@ -6,39 +6,39 @@
 block discarded – undo
6 6
 
7 7
 class Delete extends Command
8 8
 {
9
-    /**
10
-     * Execute the Delete Statement.
11
-     *
12
-     * @throws MappingException
13
-     * @throws \InvalidArgumentException
14
-     *
15
-     * @return false|void
16
-     */
17
-    public function execute()
18
-    {
19
-        $aggregate = $this->aggregate;
9
+	/**
10
+	 * Execute the Delete Statement.
11
+	 *
12
+	 * @throws MappingException
13
+	 * @throws \InvalidArgumentException
14
+	 *
15
+	 * @return false|void
16
+	 */
17
+	public function execute()
18
+	{
19
+		$aggregate = $this->aggregate;
20 20
 
21
-        $entity = $aggregate->getEntityObject();
21
+		$entity = $aggregate->getEntityObject();
22 22
 
23
-        $mapper = $aggregate->getMapper();
23
+		$mapper = $aggregate->getMapper();
24 24
 
25
-        if ($mapper->fireEvent('deleting', $entity) === false) {
26
-            return false;
27
-        }
25
+		if ($mapper->fireEvent('deleting', $entity) === false) {
26
+			return false;
27
+		}
28 28
 
29
-        $keyName = $aggregate->getEntityMap()->getKeyName();
29
+		$keyName = $aggregate->getEntityMap()->getKeyName();
30 30
 
31
-        $id = $this->aggregate->getEntityId();
31
+		$id = $this->aggregate->getEntityId();
32 32
 
33
-        if (is_null($id)) {
34
-            throw new MappingException('Executed a delete command on an entity with "null" as primary key');
35
-        }
33
+		if (is_null($id)) {
34
+			throw new MappingException('Executed a delete command on an entity with "null" as primary key');
35
+		}
36 36
 
37
-        $this->query->where($keyName, '=', $id)->delete();
37
+		$this->query->where($keyName, '=', $id)->delete();
38 38
 
39
-        $mapper->fireEvent('deleted', $entity, false);
39
+		$mapper->fireEvent('deleted', $entity, false);
40 40
 
41
-        // Once the Entity is successfully deleted, we'll just set the primary key to null.
42
-        $aggregate->setEntityAttribute($keyName, null);
43
-    }
41
+		// Once the Entity is successfully deleted, we'll just set the primary key to null.
42
+		$aggregate->setEntityAttribute($keyName, null);
43
+	}
44 44
 }
Please login to merge, or discard this patch.
src/Commands/Store.php 1 patch
Indentation   +240 added lines, -240 removed lines patch added patch discarded remove patch
@@ -14,247 +14,247 @@
 block discarded – undo
14 14
  */
15 15
 class Store extends Command
16 16
 {
17
-    /**
18
-     * Persist the entity in the database.
19
-     *
20
-     * @throws \InvalidArgumentException
21
-     *
22
-     * @return false|mixed
23
-     */
24
-    public function execute()
25
-    {
26
-        $entity = $this->aggregate->getEntityObject();
27
-
28
-        $mapper = $this->aggregate->getMapper();
29
-
30
-        if ($mapper->fireEvent('storing', $entity) === false) {
31
-            return false;
32
-        }
33
-
34
-        $this->preStoreProcess();
35
-
36
-        /*
17
+	/**
18
+	 * Persist the entity in the database.
19
+	 *
20
+	 * @throws \InvalidArgumentException
21
+	 *
22
+	 * @return false|mixed
23
+	 */
24
+	public function execute()
25
+	{
26
+		$entity = $this->aggregate->getEntityObject();
27
+
28
+		$mapper = $this->aggregate->getMapper();
29
+
30
+		if ($mapper->fireEvent('storing', $entity) === false) {
31
+			return false;
32
+		}
33
+
34
+		$this->preStoreProcess();
35
+
36
+		/*
37 37
          * We will test the entity for existence
38 38
          * and run a creation if it doesn't exists
39 39
          */
40
-        if (!$this->aggregate->exists()) {
41
-            if ($mapper->fireEvent('creating', $entity) === false) {
42
-                return false;
43
-            }
44
-
45
-            $this->insert();
46
-
47
-            $mapper->fireEvent('created', $entity, false);
48
-        } elseif ($this->aggregate->isDirty()) {
49
-            if ($mapper->fireEvent('updating', $entity) === false) {
50
-                return false;
51
-            }
52
-            $this->update();
53
-
54
-            $mapper->fireEvent('updated', $entity, false);
55
-        }
56
-
57
-        $this->postStoreProcess();
58
-
59
-        $mapper->fireEvent('stored', $entity, false);
60
-
61
-        // Then we need to hydrate the actual entity
62
-        $this->aggregate->hydrate();
63
-
64
-        return $entity;
65
-    }
66
-
67
-    /**
68
-     * Run all operations that have to occur before actually
69
-     * storing the entity.
70
-     *
71
-     * @throws \InvalidArgumentException
72
-     *
73
-     * @return void
74
-     */
75
-    protected function preStoreProcess()
76
-    {
77
-        // Create any related object that doesn't exist in the database.
78
-        $localRelationships = $this->aggregate->getEntityMap()->getLocalRelationships();
79
-
80
-        $this->createRelatedEntities($localRelationships);
81
-
82
-        // Now we can sync the related collections
83
-        $this->aggregate->syncRelationships($localRelationships);
84
-    }
85
-
86
-    /**
87
-     * Check for existence and create non-existing related entities.
88
-     *
89
-     * @param  array
90
-     *
91
-     * @throws \InvalidArgumentException
92
-     *
93
-     * @return void
94
-     */
95
-    protected function createRelatedEntities($relations)
96
-    {
97
-        $entitiesToCreate = $this->aggregate->getNonExistingRelated($relations);
98
-
99
-        foreach ($entitiesToCreate as $aggregate) {
100
-            $this->createStoreCommand($aggregate)->execute();
101
-        }
102
-    }
103
-
104
-    /**
105
-     * Create a new store command.
106
-     *
107
-     * @param Aggregate $aggregate
108
-     *
109
-     * @return Store
110
-     */
111
-    protected function createStoreCommand(Aggregate $aggregate)
112
-    {
113
-        // We gotta retrieve the corresponding query adapter to use.
114
-        $mapper = $aggregate->getMapper();
115
-
116
-        return new self($aggregate, $mapper->newQueryBuilder());
117
-    }
118
-
119
-    /**
120
-     * Run all operations that have to occur after the entity
121
-     * is stored.
122
-     *
123
-     * @throws \InvalidArgumentException
124
-     *
125
-     * @return void
126
-     */
127
-    protected function postStoreProcess()
128
-    {
129
-        $aggregate = $this->aggregate;
130
-
131
-        // Create any related object that doesn't exist in the database.
132
-        $foreignRelationships = $aggregate->getEntityMap()->getForeignRelationships();
133
-
134
-        $this->createRelatedEntities($foreignRelationships);
135
-
136
-        // Update any pivot tables that has been modified.
137
-        $aggregate->updatePivotRecords();
138
-
139
-        // Update any dirty relationship. This include relationships that already exists, have
140
-        // dirty attributes / newly created related entities / dirty related entities.
141
-        $dirtyRelatedAggregates = $aggregate->getDirtyRelationships();
142
-
143
-        foreach ($dirtyRelatedAggregates as $related) {
144
-            $this->createStoreCommand($related)->execute();
145
-        }
146
-
147
-        // Now we can sync the related collections
148
-        //
149
-        // TODO (note) : not sute this check is needed, as we can assume
150
-        // the aggregate exists in the Post Store Process
151
-        if ($this->aggregate->exists()) {
152
-            $this->aggregate->syncRelationships($foreignRelationships);
153
-        }
154
-
155
-        // TODO be move it to the wrapper class
156
-        // so it's the same code for the entity builder
157
-        $aggregate->setProxies();
158
-
159
-        // Update Entity Cache
160
-        $aggregate->getMapper()->getEntityCache()->refresh($aggregate);
161
-    }
162
-
163
-    /**
164
-     * Update Related Entities which attributes have
165
-     * been modified.
166
-     *
167
-     * @return void
168
-     */
169
-    protected function updateDirtyRelated()
170
-    {
171
-        $relations = $this->entityMap->getRelationships();
172
-        $attributes = $this->getAttributes();
173
-
174
-        foreach ($relations as $relation) {
175
-            if (!array_key_exists($relation, $attributes)) {
176
-                continue;
177
-            }
178
-
179
-            $value = $attributes[$relation];
180
-
181
-            if ($value == null) {
182
-                continue;
183
-            }
184
-
185
-            if ($value instanceof EntityProxy) {
186
-                continue;
187
-            }
188
-
189
-            if ($value instanceof CollectionProxy && $value->isLoaded()) {
190
-                $value = $value->getUnderlyingCollection();
191
-            }
192
-            if ($value instanceof CollectionProxy && !$value->isLoaded()) {
193
-                foreach ($value->getAddedItems() as $entity) {
194
-                    $this->updateEntityIfDirty($entity);
195
-                }
196
-                continue;
197
-            }
198
-
199
-            if ($value instanceof EntityCollection) {
200
-                foreach ($value as $entity) {
201
-                    if (!$this->createEntityIfNotExists($entity)) {
202
-                        $this->updateEntityIfDirty($entity);
203
-                    }
204
-                }
205
-                continue;
206
-            }
207
-            if ($value instanceof Mappable) {
208
-                $this->updateEntityIfDirty($value);
209
-                continue;
210
-            }
211
-        }
212
-    }
213
-
214
-    /**
215
-     * Execute an insert statement on the database.
216
-     *
217
-     * @return void
218
-     */
219
-    protected function insert()
220
-    {
221
-        $aggregate = $this->aggregate;
222
-
223
-        $attributes = $aggregate->getRawAttributes();
224
-
225
-        $keyName = $aggregate->getEntityMap()->getKeyName();
226
-
227
-        // Check if the primary key is defined in the attributes
228
-        if (array_key_exists($keyName, $attributes) && $attributes[$keyName] != null) {
229
-            $this->query->insert($attributes);
230
-        } else {
231
-            $sequence = $aggregate->getEntityMap()->getSequence();
232
-
233
-            $id = $this->query->insertGetId($attributes, $sequence);
234
-
235
-            $aggregate->setEntityAttribute($keyName, $id);
236
-        }
237
-    }
238
-
239
-    /**
240
-     * Run an update statement on the entity.
241
-     *
242
-     * @throws \InvalidArgumentException
243
-     *
244
-     * @return void
245
-     */
246
-    protected function update()
247
-    {
248
-        $query = $this->query;
249
-
250
-        $keyName = $this->aggregate->getEntityKey();
251
-
252
-        $query = $query->where($keyName, '=', $this->aggregate->getEntityId());
253
-
254
-        $dirtyAttributes = $this->aggregate->getDirtyRawAttributes();
255
-
256
-        if (count($dirtyAttributes) > 0) {
257
-            $query->update($dirtyAttributes);
258
-        }
259
-    }
40
+		if (!$this->aggregate->exists()) {
41
+			if ($mapper->fireEvent('creating', $entity) === false) {
42
+				return false;
43
+			}
44
+
45
+			$this->insert();
46
+
47
+			$mapper->fireEvent('created', $entity, false);
48
+		} elseif ($this->aggregate->isDirty()) {
49
+			if ($mapper->fireEvent('updating', $entity) === false) {
50
+				return false;
51
+			}
52
+			$this->update();
53
+
54
+			$mapper->fireEvent('updated', $entity, false);
55
+		}
56
+
57
+		$this->postStoreProcess();
58
+
59
+		$mapper->fireEvent('stored', $entity, false);
60
+
61
+		// Then we need to hydrate the actual entity
62
+		$this->aggregate->hydrate();
63
+
64
+		return $entity;
65
+	}
66
+
67
+	/**
68
+	 * Run all operations that have to occur before actually
69
+	 * storing the entity.
70
+	 *
71
+	 * @throws \InvalidArgumentException
72
+	 *
73
+	 * @return void
74
+	 */
75
+	protected function preStoreProcess()
76
+	{
77
+		// Create any related object that doesn't exist in the database.
78
+		$localRelationships = $this->aggregate->getEntityMap()->getLocalRelationships();
79
+
80
+		$this->createRelatedEntities($localRelationships);
81
+
82
+		// Now we can sync the related collections
83
+		$this->aggregate->syncRelationships($localRelationships);
84
+	}
85
+
86
+	/**
87
+	 * Check for existence and create non-existing related entities.
88
+	 *
89
+	 * @param  array
90
+	 *
91
+	 * @throws \InvalidArgumentException
92
+	 *
93
+	 * @return void
94
+	 */
95
+	protected function createRelatedEntities($relations)
96
+	{
97
+		$entitiesToCreate = $this->aggregate->getNonExistingRelated($relations);
98
+
99
+		foreach ($entitiesToCreate as $aggregate) {
100
+			$this->createStoreCommand($aggregate)->execute();
101
+		}
102
+	}
103
+
104
+	/**
105
+	 * Create a new store command.
106
+	 *
107
+	 * @param Aggregate $aggregate
108
+	 *
109
+	 * @return Store
110
+	 */
111
+	protected function createStoreCommand(Aggregate $aggregate)
112
+	{
113
+		// We gotta retrieve the corresponding query adapter to use.
114
+		$mapper = $aggregate->getMapper();
115
+
116
+		return new self($aggregate, $mapper->newQueryBuilder());
117
+	}
118
+
119
+	/**
120
+	 * Run all operations that have to occur after the entity
121
+	 * is stored.
122
+	 *
123
+	 * @throws \InvalidArgumentException
124
+	 *
125
+	 * @return void
126
+	 */
127
+	protected function postStoreProcess()
128
+	{
129
+		$aggregate = $this->aggregate;
130
+
131
+		// Create any related object that doesn't exist in the database.
132
+		$foreignRelationships = $aggregate->getEntityMap()->getForeignRelationships();
133
+
134
+		$this->createRelatedEntities($foreignRelationships);
135
+
136
+		// Update any pivot tables that has been modified.
137
+		$aggregate->updatePivotRecords();
138
+
139
+		// Update any dirty relationship. This include relationships that already exists, have
140
+		// dirty attributes / newly created related entities / dirty related entities.
141
+		$dirtyRelatedAggregates = $aggregate->getDirtyRelationships();
142
+
143
+		foreach ($dirtyRelatedAggregates as $related) {
144
+			$this->createStoreCommand($related)->execute();
145
+		}
146
+
147
+		// Now we can sync the related collections
148
+		//
149
+		// TODO (note) : not sute this check is needed, as we can assume
150
+		// the aggregate exists in the Post Store Process
151
+		if ($this->aggregate->exists()) {
152
+			$this->aggregate->syncRelationships($foreignRelationships);
153
+		}
154
+
155
+		// TODO be move it to the wrapper class
156
+		// so it's the same code for the entity builder
157
+		$aggregate->setProxies();
158
+
159
+		// Update Entity Cache
160
+		$aggregate->getMapper()->getEntityCache()->refresh($aggregate);
161
+	}
162
+
163
+	/**
164
+	 * Update Related Entities which attributes have
165
+	 * been modified.
166
+	 *
167
+	 * @return void
168
+	 */
169
+	protected function updateDirtyRelated()
170
+	{
171
+		$relations = $this->entityMap->getRelationships();
172
+		$attributes = $this->getAttributes();
173
+
174
+		foreach ($relations as $relation) {
175
+			if (!array_key_exists($relation, $attributes)) {
176
+				continue;
177
+			}
178
+
179
+			$value = $attributes[$relation];
180
+
181
+			if ($value == null) {
182
+				continue;
183
+			}
184
+
185
+			if ($value instanceof EntityProxy) {
186
+				continue;
187
+			}
188
+
189
+			if ($value instanceof CollectionProxy && $value->isLoaded()) {
190
+				$value = $value->getUnderlyingCollection();
191
+			}
192
+			if ($value instanceof CollectionProxy && !$value->isLoaded()) {
193
+				foreach ($value->getAddedItems() as $entity) {
194
+					$this->updateEntityIfDirty($entity);
195
+				}
196
+				continue;
197
+			}
198
+
199
+			if ($value instanceof EntityCollection) {
200
+				foreach ($value as $entity) {
201
+					if (!$this->createEntityIfNotExists($entity)) {
202
+						$this->updateEntityIfDirty($entity);
203
+					}
204
+				}
205
+				continue;
206
+			}
207
+			if ($value instanceof Mappable) {
208
+				$this->updateEntityIfDirty($value);
209
+				continue;
210
+			}
211
+		}
212
+	}
213
+
214
+	/**
215
+	 * Execute an insert statement on the database.
216
+	 *
217
+	 * @return void
218
+	 */
219
+	protected function insert()
220
+	{
221
+		$aggregate = $this->aggregate;
222
+
223
+		$attributes = $aggregate->getRawAttributes();
224
+
225
+		$keyName = $aggregate->getEntityMap()->getKeyName();
226
+
227
+		// Check if the primary key is defined in the attributes
228
+		if (array_key_exists($keyName, $attributes) && $attributes[$keyName] != null) {
229
+			$this->query->insert($attributes);
230
+		} else {
231
+			$sequence = $aggregate->getEntityMap()->getSequence();
232
+
233
+			$id = $this->query->insertGetId($attributes, $sequence);
234
+
235
+			$aggregate->setEntityAttribute($keyName, $id);
236
+		}
237
+	}
238
+
239
+	/**
240
+	 * Run an update statement on the entity.
241
+	 *
242
+	 * @throws \InvalidArgumentException
243
+	 *
244
+	 * @return void
245
+	 */
246
+	protected function update()
247
+	{
248
+		$query = $this->query;
249
+
250
+		$keyName = $this->aggregate->getEntityKey();
251
+
252
+		$query = $query->where($keyName, '=', $this->aggregate->getEntityId());
253
+
254
+		$dirtyAttributes = $this->aggregate->getDirtyRawAttributes();
255
+
256
+		if (count($dirtyAttributes) > 0) {
257
+			$query->update($dirtyAttributes);
258
+		}
259
+	}
260 260
 }
Please login to merge, or discard this patch.
src/MagicGetters.php 1 patch
Indentation   +46 added lines, -46 removed lines patch added patch discarded remove patch
@@ -4,54 +4,54 @@
 block discarded – undo
4 4
 
5 5
 trait MagicGetters
6 6
 {
7
-    /**
8
-     * Contains the entity's attributes.
9
-     *
10
-     * @var array
11
-     */
12
-    protected $attributes = [];
7
+	/**
8
+	 * Contains the entity's attributes.
9
+	 *
10
+	 * @var array
11
+	 */
12
+	protected $attributes = [];
13 13
 
14
-    /**
15
-     * Dynamically retrieve attributes on the entity.
16
-     *
17
-     * @param string $key
18
-     *
19
-     * @return mixed
20
-     */
21
-    public function __get($key)
22
-    {
23
-        // When using mixed mapping, we will check
24
-        // for a class property corresponding to
25
-        // the attribute's key first.
26
-        //
27
-        // Note : this may raise issues as we may grant
28
-        // access to unwanted properties, like class dependencies.
29
-        //
30
-        // -> Solution would be to access the entityMap's $attributes, but we
31
-        // have to do this in a very efficient way.
32
-        //
33
-        // Manager::getEntityMap(get_class($this))->hasProperty()
34
-        //
35
-        // We could do the casting to array / json the same way, and it would
14
+	/**
15
+	 * Dynamically retrieve attributes on the entity.
16
+	 *
17
+	 * @param string $key
18
+	 *
19
+	 * @return mixed
20
+	 */
21
+	public function __get($key)
22
+	{
23
+		// When using mixed mapping, we will check
24
+		// for a class property corresponding to
25
+		// the attribute's key first.
26
+		//
27
+		// Note : this may raise issues as we may grant
28
+		// access to unwanted properties, like class dependencies.
29
+		//
30
+		// -> Solution would be to access the entityMap's $attributes, but we
31
+		// have to do this in a very efficient way.
32
+		//
33
+		// Manager::getEntityMap(get_class($this))->hasProperty()
34
+		//
35
+		// We could do the casting to array / json the same way, and it would
36 36
 
37
-        if (property_exists($this, $key)) {
38
-            return $this->$key;
39
-        }
37
+		if (property_exists($this, $key)) {
38
+			return $this->$key;
39
+		}
40 40
 
41
-        if (array_key_exists($key, $this->attributes)) {
42
-            return $this->attributes[$key];
43
-        }
44
-    }
41
+		if (array_key_exists($key, $this->attributes)) {
42
+			return $this->attributes[$key];
43
+		}
44
+	}
45 45
 
46
-    /**
47
-     * Determine if an attribute exists on the entity.
48
-     *
49
-     * @param string $key
50
-     *
51
-     * @return bool
52
-     */
53
-    public function __isset($key)
54
-    {
55
-        return array_key_exists($key, $this->attributes) || property_exists($this, $key);
56
-    }
46
+	/**
47
+	 * Determine if an attribute exists on the entity.
48
+	 *
49
+	 * @param string $key
50
+	 *
51
+	 * @return bool
52
+	 */
53
+	public function __isset($key)
54
+	{
55
+		return array_key_exists($key, $this->attributes) || property_exists($this, $key);
56
+	}
57 57
 }
Please login to merge, or discard this patch.
src/AnalogueServiceProvider.php 2 patches
Indentation   +42 added lines, -42 removed lines patch added patch discarded remove patch
@@ -13,58 +13,58 @@
 block discarded – undo
13 13
  */
14 14
 class AnalogueServiceProvider extends ServiceProvider
15 15
 {
16
-    /**
17
-     * Indicates if loading of the provider is deferred.
18
-     *
19
-     * @var bool
20
-     */
21
-    protected $defer = false;
16
+	/**
17
+	 * Indicates if loading of the provider is deferred.
18
+	 *
19
+	 * @var bool
20
+	 */
21
+	protected $defer = false;
22 22
 
23
-    public function boot()
24
-    {
25
-        //
26
-    }
23
+	public function boot()
24
+	{
25
+		//
26
+	}
27 27
 
28
-    /**
29
-     * Register the service provider.
30
-     *
31
-     * @return void
32
-     */
33
-    public function register()
34
-    {
35
-        $this->app->singleton('analogue', function ($app) {
36
-            $db = $app['db'];
28
+	/**
29
+	 * Register the service provider.
30
+	 *
31
+	 * @return void
32
+	 */
33
+	public function register()
34
+	{
35
+		$this->app->singleton('analogue', function ($app) {
36
+			$db = $app['db'];
37 37
 
38
-            $connectionProvider = new IlluminateConnectionProvider($db);
38
+			$connectionProvider = new IlluminateConnectionProvider($db);
39 39
 
40
-            $illuminate = new IlluminateDriver($connectionProvider);
40
+			$illuminate = new IlluminateDriver($connectionProvider);
41 41
 
42
-            $driverManager = new DriverManager();
42
+			$driverManager = new DriverManager();
43 43
 
44
-            $driverManager->addDriver($illuminate);
44
+			$driverManager->addDriver($illuminate);
45 45
 
46
-            $event = $app->make('events');
46
+			$event = $app->make('events');
47 47
 
48
-            $manager = new Manager($driverManager, $event);
48
+			$manager = new Manager($driverManager, $event);
49 49
 
50
-            $manager->registerPlugin(\Analogue\ORM\Plugins\Timestamps\TimestampsPlugin::class);
51
-            $manager->registerPlugin(\Analogue\ORM\Plugins\SoftDeletes\SoftDeletesPlugin::class);
50
+			$manager->registerPlugin(\Analogue\ORM\Plugins\Timestamps\TimestampsPlugin::class);
51
+			$manager->registerPlugin(\Analogue\ORM\Plugins\SoftDeletes\SoftDeletesPlugin::class);
52 52
 
53
-            return $manager;
54
-        });
53
+			return $manager;
54
+		});
55 55
 
56
-        $this->app->bind(Manager::class, function ($app) {
57
-            return $app->make('analogue');
58
-        });
59
-    }
56
+		$this->app->bind(Manager::class, function ($app) {
57
+			return $app->make('analogue');
58
+		});
59
+	}
60 60
 
61
-    /**
62
-     * Get the services provided by the provider.
63
-     *
64
-     * @return array
65
-     */
66
-    public function provides()
67
-    {
68
-        return ['analogue'];
69
-    }
61
+	/**
62
+	 * Get the services provided by the provider.
63
+	 *
64
+	 * @return array
65
+	 */
66
+	public function provides()
67
+	{
68
+		return ['analogue'];
69
+	}
70 70
 }
Please login to merge, or discard this patch.
Spacing   +2 added lines, -2 removed lines patch added patch discarded remove patch
@@ -32,7 +32,7 @@  discard block
 block discarded – undo
32 32
      */
33 33
     public function register()
34 34
     {
35
-        $this->app->singleton('analogue', function ($app) {
35
+        $this->app->singleton('analogue', function($app) {
36 36
             $db = $app['db'];
37 37
 
38 38
             $connectionProvider = new IlluminateConnectionProvider($db);
@@ -53,7 +53,7 @@  discard block
 block discarded – undo
53 53
             return $manager;
54 54
         });
55 55
 
56
-        $this->app->bind(Manager::class, function ($app) {
56
+        $this->app->bind(Manager::class, function($app) {
57 57
             return $app->make('analogue');
58 58
         });
59 59
     }
Please login to merge, or discard this patch.
src/Plugins/AnaloguePluginInterface.php 1 patch
Indentation   +12 added lines, -12 removed lines patch added patch discarded remove patch
@@ -4,17 +4,17 @@
 block discarded – undo
4 4
 
5 5
 interface AnaloguePluginInterface
6 6
 {
7
-    /**
8
-     * Boot the plugin.
9
-     *
10
-     * @return void
11
-     */
12
-    public function register();
7
+	/**
8
+	 * Boot the plugin.
9
+	 *
10
+	 * @return void
11
+	 */
12
+	public function register();
13 13
 
14
-    /**
15
-     * Get custom events provided by the plugin.
16
-     *
17
-     * @return array
18
-     */
19
-    public function getCustomEvents();
14
+	/**
15
+	 * Get custom events provided by the plugin.
16
+	 *
17
+	 * @return array
18
+	 */
19
+	public function getCustomEvents();
20 20
 }
Please login to merge, or discard this patch.
src/Plugins/AnaloguePlugin.php 1 patch
Indentation   +27 added lines, -27 removed lines patch added patch discarded remove patch
@@ -6,34 +6,34 @@
 block discarded – undo
6 6
 
7 7
 abstract class AnaloguePlugin implements AnaloguePluginInterface
8 8
 {
9
-    /**
10
-     * Manager instance.
11
-     *
12
-     * @var Manager
13
-     */
14
-    protected $manager;
9
+	/**
10
+	 * Manager instance.
11
+	 *
12
+	 * @var Manager
13
+	 */
14
+	protected $manager;
15 15
 
16
-    /**
17
-     * AnaloguePlugin constructor.
18
-     *
19
-     * @param Manager $manager
20
-     */
21
-    public function __construct(Manager $manager)
22
-    {
23
-        $this->manager = $manager;
24
-    }
16
+	/**
17
+	 * AnaloguePlugin constructor.
18
+	 *
19
+	 * @param Manager $manager
20
+	 */
21
+	public function __construct(Manager $manager)
22
+	{
23
+		$this->manager = $manager;
24
+	}
25 25
 
26
-    /**
27
-     * Boot the plugin.
28
-     *
29
-     * @return void
30
-     */
31
-    abstract public function register();
26
+	/**
27
+	 * Boot the plugin.
28
+	 *
29
+	 * @return void
30
+	 */
31
+	abstract public function register();
32 32
 
33
-    /**
34
-     * Get custom events provided by the plugin.
35
-     *
36
-     * @return array
37
-     */
38
-    abstract public function getCustomEvents();
33
+	/**
34
+	 * Get custom events provided by the plugin.
35
+	 *
36
+	 * @return array
37
+	 */
38
+	abstract public function getCustomEvents();
39 39
 }
Please login to merge, or discard this patch.