Completed
Push — master ( 75326d...ecee01 )
by Ariel
08:37
created

ContactPresenter::fullname()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
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 1
            'lastname'       => 7,
38 1
            'nin'            => 10,
39 1
            'birthdate'      => 5,
40 1
            'mobile'         => 20,
41 1
            'email'          => 15,
42 1
            'postal_address' => 15,
43 1
            'user'           => 25,
44 1
        ];
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 1
            }
52 1
        }
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