Completed
Push — master ( 43f0d4...1b2d9c )
by Ariel
13:00
created

Addressbook::reuseExisting()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 8
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 8
loc 8
ccs 0
cts 7
cp 0
rs 9.4285
cc 2
eloc 4
nc 2
nop 1
crap 6
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);
45 1
        $this->business->save();
46
47 1
        if (array_key_exists('notes', $data)) {
48
            $this->updateNotes($contact, $data['notes']);
49
        }
50
51 1
        return $contact;
52
    }
53
54 1
    public function update(Contact $contact, $data = [], $notes = null)
55
    {
56 1
        $birthdate = array_get($data, 'birthdate');
57 1
        $this->sanitizeDate($birthdate);
58
59 1
        $contact->firstname = array_get($data, 'firstname');
0 ignored issues
show
Documentation introduced by
The property firstname 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...
60 1
        $contact->lastname = array_get($data, 'lastname');
0 ignored issues
show
Documentation introduced by
The property lastname 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...
61 1
        $contact->email = array_get($data, 'email');
0 ignored issues
show
Documentation introduced by
The property email 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...
62 1
        $contact->nin = array_get($data, 'nin');
0 ignored issues
show
Documentation introduced by
The property nin 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...
63 1
        $contact->gender = array_get($data, 'gender');
0 ignored issues
show
Documentation introduced by
The property gender 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...
64 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...
65 1
        $contact->mobile = array_get($data, 'mobile');
0 ignored issues
show
Documentation introduced by
The property mobile 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...
66 1
        $contact->mobile_country = array_get($data, 'mobile_country');
0 ignored issues
show
Documentation introduced by
The property mobile_country 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...
67 1
        $contact->postal_address = array_get($data, 'postal_address');
0 ignored issues
show
Documentation introduced by
The property postal_address 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...
68
69 1
        $contact->save();
70
71 1
        $this->updateNotes($contact, $notes);
72
73 1
        return $contact;
74
    }
75
76 2
    public function getSubscribed($email)
77
    {
78 2
        if (trim($email) == '') {
79
            return false;
80
        }
81
82 2
        return $this->business->contacts()->where('email', '=', $email)->first();
83
    }
84
85 1
    public function getExisting($email)
86
    {
87 1
        if (trim($email) == '') {
88
            return false;
89
        }
90
91 1
        return Contact::whereNotNull('user_id')->where('email', '=', $email)->first();
92
    }
93
94 1
    public function getRegisteredUserId($userId)
95
    {
96 1
        return $this->business->contacts()->where('user_id', '=', $userId)->first();
97
    }
98
99 1
    public function remove(Contact $contact)
100
    {
101 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...
102
    }
103
104 1
    public function linkToUserId(Contact $contact, $userId)
105
    {
106 1
        $contact->user()->associate($userId);
107 1
        $contact->save();
108
109 1
        return $contact->fresh();
110
    }
111
112 1
    public function copyFrom(Contact $contact, $userId)
113
    {
114 1
        $replicatedContact = $contact->replicate(['id']);
115 1
        $replicatedContact->user()->associate($userId);
116 1
        $replicatedContact->businesses()->detach();
117 1
        $replicatedContact->save();
118
119 1
        $this->business->contacts()->attach($replicatedContact);
120 1
        $this->business->save();
121
122 1
        return $replicatedContact;
123
    }
124
125 1
    protected function updateNotes(Contact $contact, $notes = null)
126
    {
127 1
        if ($notes) {
128
            $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...
129
        }
130 1
    }
131
132
    protected function getDateFormat()
133
    {
134
        return $this->business->pref('date_format');
135
    }
136
137 2
    protected function sanitizeDate(&$value)
138
    {
139 2
        if (!is_string($value)) {
140
            return $value;
141
        }
142
143 2
        if (trim($value) == '') {
144
            return $value = null;
145
        }
146
147 2
        if (strlen($value) == 19) {
148 2
            return $value = Carbon::parse($value);
149
        }
150
151
        if (strlen($value) == 10) {
152
            return $value = Carbon::createFromFormat('m/d/Y', $value);
153
        }
154
    }
155
}
156