Completed
Push — master ( 54f4c1...43f0d4 )
by Ariel
19:54
created

Addressbook   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 154
Duplicated Lines 10.39 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 25
c 1
b 0
f 1
lcom 1
cbo 4
dl 16
loc 154
ccs 0
cts 116
cp 0
rs 10

15 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A listing() 0 4 1
A find() 0 4 1
A register() 0 21 3
A update() 0 21 1
A reuseExisting() 8 8 2
A getExisting() 0 8 2
A getSubscribed() 8 8 2
A getRegisteredUserId() 0 4 1
A reuseExistingByUserId() 0 4 1
A remove() 0 4 1
A linkToUserId() 0 7 1
A copyFrom() 0 14 1
A updateNotes() 0 6 2
B sanitizeDate() 0 18 5

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
    public function __construct($business)
18
    {
19
        $this->business = $business;
20
    }
21
22
    public function listing($limit)
23
    {
24
        return $this->business->contacts()->orderBy('lastname', 'ASC')->simplePaginate($limit);
25
    }
26
27
    public function find(Contact $contact)
28
    {
29
        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
    public function register($data)
33
    {
34
        $contact = $this->reuseExisting($data['email']);
35
36
        if ($contact) {
37
            return $contact;
38
        }
39
40
        $this->sanitizeDate($data['birthdate']);
41
42
        $contact = Contact::create($data);
43
44
        $this->business->contacts()->attach($contact);
45
        $this->business->save();
46
47
        if (array_key_exists('notes', $data)) {
48
            $this->updateNotes($contact, $data['notes']);
49
        }
50
51
        return $contact;
52
    }
53
54
    public function update(Contact $contact, $data = [], $notes = null)
55
    {
56
        $birthdate = array_get($data, 'birthdate');
57
        $this->sanitizeDate($birthdate);
58
59
        $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
        $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
        $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
        $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
        $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
        $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
        $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
        $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
        $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
        $contact->save();
70
71
        $this->updateNotes($contact, $notes);
72
73
        return $contact;
74
    }
75
76 View Code Duplication
    public function reuseExisting($email)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
77
    {
78
        if (trim($email) == '') {
79
            return false;
80
        }
81
82
        return $this->business->contacts()->where('email', '=', $email)->first();
83
    }
84
85
    public function getExisting($email)
86
    {
87
        if (trim($email) == '') {
88
            return false;
89
        }
90
91
        return Contact::whereNotNull('user_id')->where('email', '=', $email)->first();
92
    }
93
94 View Code Duplication
    public function getSubscribed($email)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
95
    {
96
        if (trim($email) == '') {
97
            return false;
98
        }
99
100
        return $this->business->contacts()->where('email', '=', $email)->first();
101
    }
102
103
    public function getRegisteredUserId($userId)
104
    {
105
        return $this->business->contacts()->where('user_id', '=', $userId)->first();
106
    }
107
108
    public function reuseExistingByUserId($userId)
109
    {
110
        return $this->business->contacts()->where('user_id', '=', $userId)->first();
111
    }
112
113
    public function remove(Contact $contact)
114
    {
115
        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...
116
    }
117
118
    public function linkToUserId(Contact $contact, $userId)
119
    {
120
        $contact->user()->associate($userId);
121
        $contact->save();
122
123
        return $contact->fresh();
124
    }
125
126
    public function copyFrom(Contact $existingContact, $userId)
127
    {
128
        $existingContactData = $existingContact->toArray();
129
        $this->sanitizeDate($existingContactData['birthdate']);
130
        $contact = Contact::create($existingContactData);
131
        $contact->user()->associate($userId);
132
        $contact->businesses()->detach();
133
        $contact->save();
134
135
        $this->business->contacts()->attach($contact);
136
        $this->business->save();
137
138
        return $contact;
139
    }
140
141
    protected function updateNotes(Contact $contact, $notes = null)
142
    {
143
        if ($notes) {
144
            $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...
145
        }
146
    }
147
148
    protected function sanitizeDate(&$value)
149
    {
150
        if (!is_string($value)) {
151
            return $value;
152
        }
153
154
        if (trim($value) == '') {
155
            return $value = null;
156
        }
157
158
        if (strlen($value) == 19) {
159
            return $value = Carbon::parse($value);
160
        }
161
162
        if (strlen($value) == 10) {
163
            return $value = Carbon::createFromFormat(trans('app.dateformat.carbon'), $value);
0 ignored issues
show
Bug introduced by
It seems like trans('app.dateformat.carbon') targeting trans() can also be of type object<Symfony\Component...on\TranslatorInterface>; however, Carbon\Carbon::createFromFormat() does only seem to accept string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
164
        }
165
    }
166
}
167