| 1 | <?php |
||
| 2 | /** |
||
| 3 | * This file is part of the sauls/object-registry-bundle package. |
||
| 4 | * |
||
| 5 | * @author Saulius Vaičeliūnas <[email protected]> |
||
| 6 | * @link http://saulius.vaiceliunas.lt |
||
| 7 | * @copyright 2018 |
||
| 8 | * |
||
| 9 | * For the full copyright and license information, please view the LICENSE |
||
| 10 | * file that was distributed with this source code. |
||
| 11 | */ |
||
| 12 | |||
| 13 | |||
| 14 | namespace Sauls\Bundle\ObjectRegistryBundle\Event; |
||
| 15 | |||
| 16 | use Doctrine\Common\EventArgs; |
||
| 17 | use Doctrine\ORM\EntityManagerInterface; |
||
| 18 | use Doctrine\ORM\UnitOfWork; |
||
| 19 | |||
| 20 | class GenericDoctrineObjectEvent extends GenericObjectEvent |
||
| 21 | { |
||
| 22 | /** |
||
| 23 | * @var EventArgs |
||
| 24 | */ |
||
| 25 | private $parent; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var array |
||
| 29 | */ |
||
| 30 | private $changeSet; |
||
| 31 | |||
| 32 | 13 | public function __construct(object $subject = null, EventArgs $parent, array $arguments = array()) |
|
| 33 | { |
||
| 34 | 13 | parent::__construct($subject, $arguments); |
|
| 35 | |||
| 36 | 13 | $this->parent = $parent; |
|
| 37 | 13 | } |
|
| 38 | |||
| 39 | 1 | public function getEntity(): object |
|
| 40 | { |
||
| 41 | 1 | return $this->getSubject(); |
|
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 42 | } |
||
| 43 | |||
| 44 | 2 | public function getParent(): EventArgs |
|
| 45 | { |
||
| 46 | 2 | return $this->parent; |
|
| 47 | } |
||
| 48 | |||
| 49 | 1 | public function setParent(EventArgs $parent): void |
|
| 50 | { |
||
| 51 | 1 | $this->parent = $parent; |
|
| 52 | 1 | } |
|
| 53 | |||
| 54 | /** |
||
| 55 | * Get the entire changeset (or just chengeset of an attribute if key is setted). |
||
| 56 | * |
||
| 57 | * @param string|null $key |
||
| 58 | * |
||
| 59 | * @return array |
||
| 60 | */ |
||
| 61 | 3 | public function getChangeSet($key = null) |
|
| 62 | { |
||
| 63 | 3 | $changeSet = $this->registerChangeSet(); |
|
| 64 | 3 | if (null === $key) { |
|
| 65 | 1 | return $changeSet; |
|
| 66 | } |
||
| 67 | 2 | if (false === array_key_exists($key, $changeSet)) { |
|
| 68 | 1 | return false; |
|
|
0 ignored issues
–
show
|
|||
| 69 | } |
||
| 70 | 1 | return $changeSet[$key]; |
|
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @return array |
||
| 75 | */ |
||
| 76 | 3 | private function registerChangeSet() |
|
| 77 | { |
||
| 78 | 3 | return $this->changeSet = null === $this->changeSet |
|
| 79 | 3 | ? $this->getUnitOfWork()->getEntityChangeSet($this->subject) |
|
| 80 | 3 | : $this->changeSet; |
|
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @return UnitOfWork |
||
| 85 | */ |
||
| 86 | 3 | public function getUnitOfWork() |
|
| 87 | { |
||
| 88 | 3 | return $this->getEntityManager()->getUnitOfWork(); |
|
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @return EntityManagerInterface |
||
| 93 | */ |
||
| 94 | 4 | public function getEntityManager(): EntityManagerInterface |
|
| 95 | { |
||
| 96 | 4 | if (\method_exists($this->parent, 'getEntityManager')) { |
|
| 97 | 1 | return $this->parent->getEntityManager(); |
|
| 98 | } |
||
| 99 | |||
| 100 | 3 | if (\method_exists($this->parent, 'getObjectManager')) { |
|
| 101 | 2 | return $this->parent->getObjectManager(); |
|
| 102 | } |
||
| 103 | |||
| 104 | 1 | throw new \RuntimeException(sprintf( |
|
| 105 | 1 | 'Method `getEntityManager` or `getObjectManager` does not exist on parent event class `%s`', |
|
| 106 | 1 | \get_class($this->parent)) |
|
| 107 | ); |
||
| 108 | } |
||
| 109 | } |
||
| 110 |