Passed
Pull Request — master (#8)
by Derek Stephen
10:36
created

PersonService::populateFromArray()   F

Complexity

Conditions 11
Paths 1024

Size

Total Lines 14
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 11

Importance

Changes 3
Bugs 0 Features 1
Metric Value
eloc 11
c 3
b 0
f 1
dl 0
loc 14
ccs 3
cts 3
cp 1
rs 3.15
cc 11
nc 1024
nop 2
crap 11

How to fix   Complexity   

Long Method

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:

1
<?php
2
3
namespace Del\Person\Service;
4
5
use Del\Person\Criteria\PersonCriteria;
6
use Del\Person\Entity\Person;
7
use Del\Person\Repository\PersonRepository;
8
use Doctrine\ORM\EntityManager;
9
use Barnacle\Container;
10
11
class PersonService
12
{
13
    public function __construct(
14
        protected EntityManager $em
15
    ) {
16
    }
17
18
    public function createFromArray(array $data): Person
19
    {
20 5
        $person = new Person();
21
22 5
        return $this->populateFromArray($person, $data);
23 5
    }
24
25
    public function populateFromArray(Person $person, array $data): Person
26
    {
27
        isset($data['id']) ? $person->setId($data['id']) : null;
28
        isset($data['firstname']) ? $person->setFirstname($data['firstname']) : null;
29 4
        isset($data['middlename']) ? $person->setMiddlename($data['middlename']) : null;
30
        isset($data['lastname']) ? $person->setLastname($data['lastname']) : null;
31 4
        isset($data['aka']) ? $person->setAka($data['aka']) : null;
32
        isset($data['dob']) ? $person->setDob($data['dob']) : null;
33 4
        isset($data['birthplace']) ? $person->setBirthplace($data['birthplace']) : null;
34
        isset($data['country']) ? $person->setCountry($data['country']) : null;
35
        isset($data['image']) ? $person->setImage($data['image']) : null;
36
        isset($data['backgroundImage']) ? $person->setImage($data['image']) : null;
37
38
        return $person;
39
    }
40
41 4
    public function toArray(Person $person): array
42
    {
43 4
        $data = [
44 4
            'id' => $person->getId(),
45 4
            'firstname' => $person->getFirstname(),
46 4
            'middlename' => $person->getMiddlename(),
47 4
            'lastname' => $person->getLastname(),
48 4
            'aka' => $person->getAka(),
49 4
            'dob' => $person->getDob(),
50 4
            'birthplace' => $person->getBirthplace(),
51 4
            'country' => $person->getCountry(),
52
            'image' => $person->getImage(),
53 4
            'backgroundImage' => $person->getBackgroundImage(),
54
        ];
55
56
        return $data;
57
    }
58
59
    public function savePerson(Person $person): Person
60 1
    {
61
        return $this->getRepository()->save($person);
62 1
    }
63 1
64 1
    public function deletePerson(Person $person): void
65 1
    {
66 1
        $this->getRepository()->delete($person);
67 1
    }
68 1
69 1
    public function findByCriteria(PersonCriteria $criteria): array
70 1
    {
71 1
        return $this->getRepository()->findByCriteria($criteria);
72
    }
73 1
74
    public function findOneByCriteria(PersonCriteria $criteria): ?Person
75
    {
76
        $results = $this->findByCriteria($criteria);
77
        return (isset($results[0])) ? $results[0] : null;
78
    }
79
80 2
    protected function getRepository(): PersonRepository
81
    {
82 2
        return $this->em->getRepository(Person::class);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->em->getRep...n\Entity\Person::class) returns the type Doctrine\Persistence\ObjectRepository which includes types incompatible with the type-hinted return Del\Person\Repository\PersonRepository.
Loading history...
83
    }
84
}
85