| Conditions | 3 |
| Paths | 3 |
| Total Lines | 24 |
| Code Lines | 18 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 0 |
| CRAP Score | 12 |
| Changes | 0 | ||
| 1 | <?php |
||
| 28 | public function fromPermission(Permission $permission): History |
||
| 29 | { |
||
| 30 | $state = $this->entityManager->getUnitOfWork()->getEntityState($permission); |
||
| 31 | |||
| 32 | switch ($state) { |
||
| 33 | case UnitOfWork::STATE_MANAGED: |
||
| 34 | $action = self::ACTION_ADD; |
||
| 35 | break; |
||
| 36 | case UnitOfWork::STATE_REMOVED: |
||
| 37 | $action = self::ACTION_REMOVE; |
||
| 38 | break; |
||
| 39 | default: |
||
| 40 | $action = self::ACTION_UNKNOWN; |
||
| 41 | } |
||
| 42 | |||
| 43 | $history = new History(); |
||
| 44 | $history->setChangeAction($action); |
||
| 45 | $history->setPermission($permission); |
||
| 46 | $history->setResource($permission->getResource()); |
||
| 47 | $history->setUser($permission->getUser()); |
||
| 48 | $history->setAt(new \DateTime()); |
||
| 49 | |||
| 50 | return $history; |
||
| 51 | } |
||
| 52 | } |
||
| 53 |
The
EntityManagermight become unusable for example if a transaction is rolled back and it gets closed. Let’s assume that somewhere in your application, or in a third-party library, there is code such as the following:If that code throws an exception and the
EntityManageris closed. Any other code which depends on the same instance of theEntityManagerduring this request will fail.On the other hand, if you instead inject the
ManagerRegistry, thegetManager()method guarantees that you will always get a usable manager instance.