Passed
Pull Request — master (#8)
by Derek Stephen
07:57 queued 06:07
created

PersonService::savePerson()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 1
c 2
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
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 5
    public function __construct(
14
        protected EntityManager $em
15
    ) {
16 5
    }
17
18 4
    public function createFromArray(array $data): Person
19
    {
20 4
        $person = new Person();
21
22 4
        return $this->populateFromArray($person, $data);
23
    }
24
25 4
    public function populateFromArray(Person $person, array $data): Person
26
    {
27 4
        isset($data['id']) ? $person->setId($data['id']) : null;
28 4
        isset($data['firstname']) ? $person->setFirstname($data['firstname']) : null;
29 4
        isset($data['middlename']) ? $person->setMiddlename($data['middlename']) : null;
30 4
        isset($data['lastname']) ? $person->setLastname($data['lastname']) : null;
31 4
        isset($data['aka']) ? $person->setAka($data['aka']) : null;
32 4
        isset($data['dob']) ? $person->setDob($data['dob']) : null;
33 4
        isset($data['birthplace']) ? $person->setBirthplace($data['birthplace']) : null;
34 4
        isset($data['country']) ? $person->setCountry($data['country']) : null;
35 4
        isset($data['image']) ? $person->setImage($data['image']) : null;
36 4
        isset($data['backgroundImage']) ? $person->setImage($data['image']) : null;
37
38 4
        return $person;
39
    }
40
41 1
    public function toArray(Person $person): array
42
    {
43 1
        $data = [
44 1
            'id' => $person->getId(),
45 1
            'firstname' => $person->getFirstname(),
46 1
            'middlename' => $person->getMiddlename(),
47 1
            'lastname' => $person->getLastname(),
48 1
            'aka' => $person->getAka(),
49 1
            'dob' => $person->getDob(),
50 1
            'birthplace' => $person->getBirthplace(),
51 1
            'country' => $person->getCountry(),
52 1
            'image' => $person->getImage(),
53 1
            'backgroundImage' => $person->getBackgroundImage(),
54 1
        ];
55
56 1
        return $data;
57
    }
58
59 2
    public function savePerson(Person $person): Person
60
    {
61 2
        return $this->getRepository()->save($person);
62
    }
63
64 2
    public function deletePerson(Person $person): void
65
    {
66 2
        $this->getRepository()->delete($person);
67
    }
68
69 1
    public function findByCriteria(PersonCriteria $criteria): array
70
    {
71 1
        return $this->getRepository()->findByCriteria($criteria);
72
    }
73
74 1
    public function findOneByCriteria(PersonCriteria $criteria): ?Person
75
    {
76 1
        $results = $this->findByCriteria($criteria);
77 1
        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