Completed
Push — master ( 4f5242...1c6c97 )
by Derek Stephen
02:30
created

PersonService::createFromArray()   C

Complexity

Conditions 9
Paths 256

Size

Total Lines 13
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 9

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 13
ccs 11
cts 11
cp 1
rs 6.4615
cc 9
eloc 11
nc 256
nop 1
crap 9
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\Person as PersonRepository;
8
use Doctrine\ORM\EntityManager;
9
use Pimple\Container;
10
11
class PersonService
12
{
13
    /** @var EntityManager $em */
14
    protected $em;
15
16 5
    public function __construct(Container $c)
17
    {
18 5
        $this->em = $c['doctrine.entity_manager'];
19 5
    }
20
21
   /** 
22
    * @param array $data
23
    * @return Person
24
    */
25 4
    public function createFromArray(array $data)
26
    {
27 4
        $person = new Person();
28 4
        isset($data['id']) ? $person->setId($data['id']) : null;
29 4
        isset($data['firstname']) ? $person->setFirstname($data['firstname']) : null;
30 4
        isset($data['middlename']) ? $person->setMiddlename($data['middlename']) : null;
31 4
        isset($data['lastname']) ? $person->setLastname($data['lastname']) : null;
32 4
        isset($data['aka']) ? $person->setAka($data['aka']) : null;
33 4
        isset($data['dob']) ? $person->setDob($data['dob']) : null;
34 4
        isset($data['birthplace']) ? $person->setBirthplace($data['birthplace']) : null;
35 4
        isset($data['country']) ? $person->setCountry($data['country']) : null;
36 4
        return $person;
37
    }
38
39
    /**
40
     * @param Person $person
41
     * @return array
42
     */
43 1
    public function toArray(Person $person)
44
    {
45
        $data = [
46 1
            'id' => $person->getId(),
47 1
            'firstname' => $person->getFirstname(),
48 1
            'middlename' => $person->getMiddlename(),
49 1
            'lastname' => $person->getLastname(),
50 1
            'aka' => $person->getAka(),
51 1
            'dob' => $person->getDob(),
52 1
            'birthplace' => $person->getBirthplace(),
53 1
            'country' => $person->getCountry(),
54 1
        ];
55 1
        return $data;
56
    }
57
58
    /**
59
     * @param Person $person
60
     * @return Person
61
     */
62 2
    public function savePerson(Person $person)
63
    {
64 2
        return $this->getRepository()->save($person);
65
    }
66
67
    /**
68
     * @param Person $person
69
     * @return Person
70
     */
71 2
    public function deletePerson(Person $person)
72
    {
73 2
        $this->getRepository()->delete($person);
74 2
    }
75
76
    /**
77
     * @param PersonCriteria $criteria
78
     * @return Person[]
79
     */
80 1
    public function findByCriteria(PersonCriteria $criteria)
81
    {
82 1
        return $this->getRepository()->findByCriteria($criteria);
83
    }
84
85
    /**
86
     * @param PersonCriteria $criteria
87
     * @return Person|null
88
     */
89 1
    public function findOneByCriteria(PersonCriteria $criteria)
90
    {
91 1
        $results = $this->findByCriteria($criteria);
92 1
        return (isset($results[0])) ? $results[0] : null;
93
    }
94
95
   /**
96
    * @return PersonRepository
97
    */
98 2
    protected function getRepository()
99
    {
100 2
        return $this->em->getRepository('Del\Person\Entity\Person');
101
    }
102
}
103