Conditions | 14 |
Paths | 24 |
Total Lines | 92 |
Code Lines | 61 |
Lines | 0 |
Ratio | 0 % |
Changes | 13 | ||
Bugs | 5 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
65 | public function execute(Connection $connection) |
||
66 | { |
||
67 | $registry = $this->getRegistry(); |
||
68 | $entry = $registry->getEntry($this->entity); |
||
69 | if (false === $entry) { |
||
70 | throw new UnregisteredEntityException('Unregistered entity: '. get_class($this->entity)); |
||
71 | } |
||
72 | |||
73 | $state = $entry->getState(); |
||
74 | $table = $connection->table($registry->getTableName()); |
||
75 | $query = Query::factory(); |
||
76 | $queryParams = array(); |
||
77 | $access = new Accessor($this->entity); |
||
78 | $tableRegistry = $connection->table($registry->getTableName())->getRegistry(); |
||
79 | |||
80 | if (in_array($this->entity, self::$working, true)) { |
||
81 | return; |
||
82 | } |
||
83 | |||
84 | if ($tableRegistry !== $registry && $tableRegistry->contains($this->entity)) { |
||
85 | $state = $tableRegistry->getState($this->entity); |
||
86 | } |
||
87 | |||
88 | switch ($state) { |
||
89 | case RegistryState::UNKNOWN: |
||
90 | throw new \LogicException(sprintf('Entity is in unknown state (%s)', get_class($this->entity))); |
||
91 | |||
92 | case RegistryState::REGISTERED: |
||
93 | return; |
||
94 | |||
95 | case RegistryState::FRESH: |
||
96 | case RegistryState::CHANGED: |
||
97 | array_push(self::$working, $this->entity); |
||
98 | $registry->fireEvent( |
||
99 | $this->entity, new BeforeDeleteEvent( |
||
100 | $connection, |
||
101 | $table, |
||
102 | $this->entity |
||
103 | ) |
||
104 | ); |
||
105 | |||
106 | $changed = $registry->getChangedValues($this->entity); |
||
107 | $query->delete($table->getName())->where('1 = 1'); |
||
108 | $ids = $entry->getIdentifiers(); |
||
109 | $idKeys = $table->getIdentifiersKeys(); |
||
110 | |||
111 | if (!count($ids)) { |
||
112 | static::removeFromWorking($this->entity); |
||
113 | throw new \LogicException( |
||
114 | sprintf('Entity %s lacks identifiers and cannot be deleted.', get_class($this->entity)) |
||
115 | ); |
||
116 | } |
||
117 | |||
118 | foreach ($changed as $key => $value) { |
||
119 | if (\array_key_exists($key, $ids)) { |
||
120 | static::removeFromWorking($this->entity); |
||
121 | throw new \LogicException( |
||
122 | sprintf( |
||
123 | 'Unable to delete entity because identifiers (%s) have been modified', |
||
124 | implode(', ', $ids) |
||
125 | ) |
||
126 | ); |
||
127 | } |
||
128 | } |
||
129 | |||
130 | foreach ($idKeys as $key) { |
||
131 | $query->andWhere(sprintf('`%s` = ?', $key)); |
||
132 | $value = $access->get($key); |
||
133 | if (!$value) { |
||
134 | static::removeFromWorking($this->entity); |
||
135 | throw new \RuntimeException( |
||
136 | sprintf( |
||
137 | 'Cannot delete entity object (%s) because it '. |
||
138 | 'lacks identifier (%s)', |
||
139 | get_class($this->entity), |
||
140 | $key |
||
141 | ) |
||
142 | ); |
||
143 | } |
||
144 | $queryParams[] = $value; |
||
145 | } |
||
146 | |||
147 | break; |
||
148 | } |
||
149 | |||
150 | $connection->execute($query, $queryParams); |
||
151 | $registry->fireEvent( |
||
152 | $this->entity, new AfterDeleteEvent($connection, $table, $this->entity) |
||
153 | ); |
||
154 | $registry->remove($this->entity); |
||
155 | static::removeFromWorking($this->entity); |
||
156 | } |
||
157 | } |