GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#75)
by joseph
14:44
created

EntityFactory::setEntityValues()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 13
ccs 9
cts 9
cp 1
rs 10
c 0
b 0
f 0
cc 4
nc 4
nop 2
crap 4
1
<?php declare(strict_types=1);
2
3
namespace EdmondsCommerce\DoctrineStaticMeta\Entity\Factory;
4
5
use Doctrine\Common\NotifyPropertyChanged;
6
use Doctrine\ORM\EntityManagerInterface;
7
use EdmondsCommerce\DoctrineStaticMeta\Entity\Interfaces\EntityInterface;
8
use EdmondsCommerce\DoctrineStaticMeta\Entity\Validation\EntityValidatorFactory;
9
10
class EntityFactory
11
{
12
    /**
13
     * @var EntityValidatorFactory
14
     */
15
    private $entityValidatorFactory;
16
    /**
17
     * @var EntityManagerInterface
18
     */
19
    private $entityManager;
20
21 19
    public function __construct(EntityValidatorFactory $entityValidatorFactory, EntityManagerInterface $entityManager)
22
    {
23 19
        $this->entityValidatorFactory = $entityValidatorFactory;
24 19
        $this->entityManager          = $entityManager;
25 19
    }
26
27
    /**
28
     * Build a new entity with the validator factory preloaded
29
     *
30
     * Optionally pass in an array of property=>value
31
     *
32
     * @param string $entityFqn
33
     *
34
     * @param array  $values
35
     *
36
     * @return mixed
37
     */
38 18
    public function create(string $entityFqn, array $values = [])
39
    {
40 18
        $entity = $this->createEntity($entityFqn);
41 18
        $entity->ensureMetaDataIsSet($this->entityManager);
42 18
        $this->addListenerToEntityIfRequired($entity);
43 18
        $this->setEntityValues($entity, $values);
44
45 17
        return $entity;
46
    }
47
48
    /**
49
     * Create the Entity
50
     *
51
     * @param string $entityFqn
52
     *
53
     * @return EntityInterface
54
     */
55 18
    private function createEntity(string $entityFqn): EntityInterface
56
    {
57 18
        return new $entityFqn($this->entityValidatorFactory);
58
    }
59
60
    /**
61
     * Set all the values, if there are any
62
     *
63
     * @param EntityInterface $entity
64
     * @param array           $values
65
     */
66 18
    private function setEntityValues(EntityInterface $entity, array $values): void
67
    {
68 18
        if ([] === $values) {
69 16
            return;
70
        }
71 2
        foreach ($values as $property => $value) {
72 2
            $setter = 'set'.$property;
73 2
            if (!method_exists($entity, $setter)) {
74 1
                throw new \InvalidArgumentException(
75 1
                    'The entity '.\get_class($entity).' does not have the setter method '.$setter
76
                );
77
            }
78 1
            $entity->$setter($value);
79
        }
80 1
    }
81
82
    /**
83
     * Generally DSM Entities are using the Notify change tracking policy.
84
     * This ensures that they are fully set up for that
85
     *
86
     * @param EntityInterface $entity
87
     */
88 18
    private function addListenerToEntityIfRequired(EntityInterface $entity): void
89
    {
90 18
        if (!$entity instanceof NotifyPropertyChanged) {
0 ignored issues
show
introduced by
$entity is always a sub-type of Doctrine\Common\NotifyPropertyChanged.
Loading history...
91
            return;
92
        }
93 18
        $listener = $this->entityManager->getUnitOfWork();
94 18
        $entity->addPropertyChangedListener($listener);
95 18
    }
96
}
97