Completed
Push — master ( 060e1c...455993 )
by Ariel
08:01
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\Models\Contact;
7
8
class ContactPresenter extends BasePresenter
9
{
10 4
    public function __construct(Contact $resource)
11
    {
12 4
        $this->wrappedObject = $resource;
13 4
    }
14
15
    /**
16
     * get fullname.
17
     *
18
     * @return string Contact firstname and lastname
19
     */
20 1
    public function fullname()
21
    {
22 1
        return trim($this->wrappedObject->firstname.' '.$this->wrappedObject->lastname);
23
    }
24
25
    /**
26
     * TODO: Check if needs to get moved to a calculator class.
27
     *
28
     * get Quality
29
     *
30
     * @return float Contact quality percentual score calculated from profile completion
31
     */
32 1
    public function quality()
33
    {
34
        $propertiesScore = [
35 1
            'firstname'      => 3,
36 1
            'lastname'       => 7,
37 1
            'nin'            => 10,
38 1
            'birthdate'      => 5,
39 1
            'mobile'         => 20,
40 1
            'email'          => 15,
41 1
            'postal_address' => 15,
42 1
            'user'           => 25,
43 1
        ];
44 1
        $totalScore = array_sum($propertiesScore);
45
46 1
        $qualityScore = 0;
47 1
        foreach ($propertiesScore as $property => $score) {
48 1
            if (trim($this->wrappedObject->$property) !== '') {
49 1
                $qualityScore += $score;
50 1
            }
51 1
        }
52
53 1
        return ceil($qualityScore / $totalScore * 100);
54
    }
55
56
    /**
57
     * ToDo: Use Carbon instead of DateTime.
58
     *
59
     * get Age
60
     *
61
     * @return int Age in years
62
     */
63 1
    public function age()
64
    {
65 1
        if ($this->wrappedObject->birthdate == null) {
66
            return;
67
        }
68
69 1
        $reference = new \DateTime();
70 1
        $born = new \DateTime($this->wrappedObject->birthdate);
71
72 1
        if ($this->wrappedObject->birthdate > $reference) {
73
            return;
74
        }
75
76 1
        $diff = $reference->diff($born);
77
78 1
        return $diff->y;
79
    }
80
}
81