Addressbook::getDateFormat()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Timegridio\Concierge;
4
5
use Carbon\Carbon;
6
use Models\Business;
7
use Timegridio\Concierge\Models\Contact;
8
9
/**
10
 * The Addressbook class acts as a simplified Contact repository with most
11
 * common read/write functions.
12
 */
13
class Addressbook
14
{
15
    private $business;
16
17 10
    public function __construct($business)
18
    {
19 10
        $this->business = $business;
20 10
    }
21
22 1
    public function listing($limit)
23
    {
24 1
        return $this->business->contacts()->orderBy('lastname', 'ASC')->simplePaginate($limit);
25
    }
26
27 1
    public function find(Contact $contact)
28
    {
29 1
        return $this->business->contacts()->find($contact->id);
30
    }
31
32 1
    public function register($data)
33
    {
34 1
        $contact = $this->getSubscribed($data['email']);
35
36 1
        if ($contact) {
37
            return $contact;
38
        }
39
40 1
        $this->sanitizeDate($data['birthdate']);
41
42 1
        $contact = Contact::create($data);
0 ignored issues
show
Bug introduced by
The method create() does not exist on Timegridio\Concierge\Models\Contact. Did you maybe mean created()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
43
44 1
        $this->business->contacts()->attach($contact, array_only($data, 'notes'));
45
46 1
        return $contact;
47
    }
48
49 1
    public function update(Contact $contact, $data = [], $notes = null)
50
    {
51 1
        $birthdate = array_get($data, 'birthdate');
52 1
        $this->sanitizeDate($birthdate);
53
54 1
        $contact->fill(array_except($data, 'birthdate'));
55 1
        $contact->birthdate = $birthdate;
56
57 1
        $contact->save();
58
59 1
        $this->updateNotes($contact, $notes);
60
61 1
        return $contact;
62
    }
63
64 2
    public function getSubscribed($email)
65
    {
66 2
        if (trim($email) == '') {
67
            return false;
68
        }
69
70 2
        return $this->business->contacts()->where('email', '=', $email)->first();
71
    }
72
73 1
    public function getExisting($email)
74
    {
75 1
        if (trim($email) == '') {
76
            return false;
77
        }
78
79 1
        return Contact::whereNotNull('user_id')->where('email', '=', $email)->first();
80
    }
81
82 1
    public function getRegisteredUserId($userId)
83
    {
84 1
        return $this->business->contacts()->where('user_id', '=', $userId)->first();
85
    }
86
87 1
    public function remove(Contact $contact)
88
    {
89 1
        return $this->business->contacts()->detach($contact->id);
90
    }
91
92 1
    public function linkToUserId(Contact $contact, $userId)
93
    {
94 1
        $contact->user()->associate($userId);
95 1
        $contact->save();
96
97 1
        return $contact->fresh();
98
    }
99
100 1
    public function copyFrom(Contact $contact, $userId)
101
    {
102 1
        $replicatedContact = $contact->replicate(['id']);
103 1
        $replicatedContact->user()->associate($userId);
104 1
        $replicatedContact->businesses()->detach();
105 1
        $replicatedContact->save();
106
107 1
        $this->business->contacts()->attach($replicatedContact);
108 1
        $this->business->save();
109
110 1
        return $replicatedContact;
111
    }
112
113 1
    protected function updateNotes(Contact $contact, $notes)
114
    {
115 1
        $this->business->contacts()->find($contact->id)->pivot->update(compact('notes'));
116 1
    }
117
118
    protected function getDateFormat()
119
    {
120
        return $this->business->pref('date_format');
121
    }
122
123 2
    protected function sanitizeDate(&$value)
124
    {
125 2
        if (!is_string($value)) {
126
            return $value;
127
        }
128
129 2
        if (trim($value) == '') {
130
            return $value = null;
131
        }
132
133 2
        if (strlen($value) == 19) {
134 2
            return $value = Carbon::parse($value);
135
        }
136
137
        if (strlen($value) == 10) {
138
            return $value = Carbon::createFromFormat('m/d/Y', $value);
139
        }
140
    }
141
}
142