ContactPresenter::quality()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 8
cts 8
cp 1
rs 9.552
c 0
b 0
f 0
cc 3
nc 3
nop 0
crap 3
1
<?php
2
3
namespace Timegridio\Concierge\Presenters;
4
5
use McCool\LaravelAutoPresenter\BasePresenter;
6
use Timegridio\Concierge\Exceptions\InvalidContactAgeException;
7
use Timegridio\Concierge\Models\Contact;
8
9
class ContactPresenter extends BasePresenter
10
{
11 6
    public function __construct(Contact $resource)
12
    {
13 6
        $this->wrappedObject = $resource;
14 6
    }
15
16
    /**
17
     * get fullname.
18
     *
19
     * @return string Contact firstname and lastname
20
     */
21 1
    public function fullname()
22
    {
23 1
        return trim($this->wrappedObject->firstname.' '.$this->wrappedObject->lastname);
24
    }
25
26
    /**
27
     * TODO: Check if needs to get moved to a calculator class.
28
     *
29
     * get Quality
30
     *
31
     * @return float Contact quality percentual score calculated from profile completion
32
     */
33 1
    public function quality()
34
    {
35
        $propertiesScore = [
36 1
            'firstname'      => 3,
37
            'lastname'       => 7,
38
            'nin'            => 10,
39
            'birthdate'      => 5,
40
            'mobile'         => 20,
41
            'email'          => 15,
42
            'postal_address' => 15,
43
            'user'           => 25,
44
        ];
45 1
        $totalScore = array_sum($propertiesScore);
46
47 1
        $qualityScore = 0;
48 1
        foreach ($propertiesScore as $property => $score) {
49 1
            if (trim($this->wrappedObject->$property) !== '') {
50 1
                $qualityScore += $score;
51
            }
52
        }
53
54 1
        return ceil($qualityScore / $totalScore * 100);
55
    }
56
57
    /**
58
     * ToDo: Use Carbon instead of DateTime.
59
     *
60
     * get Age
61
     *
62
     * @return int Age in years
63
     */
64 3
    public function age()
65
    {
66 3
        if ($this->wrappedObject->birthdate == null) {
67 1
            return;
68
        }
69
70 2
        $reference = new \DateTime();
71 2
        $born = new \DateTime($this->wrappedObject->birthdate);
72
73 2
        if ($this->wrappedObject->birthdate > $reference) {
74 1
            throw new InvalidContactAgeException;
75
        }
76
77 1
        $diff = $reference->diff($born);
78
79 1
        return $diff->y;
80
    }
81
}
82