Completed
Push — master ( 1b2d9c...0ef0fe )
by Ariel
12:19
created

Addressbook::linkToUserId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 2
crap 1
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);
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<Timegridio\Concierge\Models\Contact>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
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);
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;
0 ignored issues
show
Documentation introduced by
The property birthdate does not exist on object<Timegridio\Concierge\Models\Contact>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
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);
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<Timegridio\Concierge\Models\Contact>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
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'));
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<Timegridio\Concierge\Models\Contact>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
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