Test Failed
Push — master ( f21b2a...0b0868 )
by Burak
06:41 queued 29s
created

ContactService   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Test Coverage

Coverage 91.67%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 18
c 1
b 0
f 0
dl 0
loc 74
ccs 22
cts 24
cp 0.9167
rs 10
wmc 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A contacts() 0 3 1
A setContacts() 0 12 1
A contact() 0 3 1
A addContact() 0 8 1
A removeContact() 0 6 1
1
<?php
2
3
namespace App\Services;
4
5
use App\Interfaces\ContactServiceInterface;
6
use App\Models\Contact;
7
use App\Models\User;
8
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
9
use Illuminate\Support\Collection;
10
11
class ContactService implements ContactServiceInterface
12
{
13
    /**
14
     * All contacts.
15
     *
16
     * @param User $user
17
     * @return LengthAwarePaginator
18
     */
19 4
    public function contacts(User $user): LengthAwarePaginator
20
    {
21 4
        return Contact::forUser($user->id)->with('source')->paginate();
22
    }
23
24
    /**
25
     * Retrieve a contact.
26
     *
27
     * @param string $contact_id
28
     * @param User $user
29
     * @return Contact|null
30
     */
31
    public function contact(string $contact_id, User $user): ?Contact
32
    {
33
        return Contact::where('id', $contact_id)->forUser($user->id)->first();
34
    }
35
36
    /**
37
     * Creates contact.
38
     * @param User $user
39
     * @param User $contact
40
     * @return Contact
41
     */
42 11
    public function addContact(User $user, User $contact): Contact
43
    {
44 11
        return Contact::updateOrCreate([
45 11
            'user_id' => $user->id,
46 11
            'source_type' => get_class($contact),
47 11
            'source_id' => $contact->id,
48
        ], [
49 11
            'name' => $contact->name,
0 ignored issues
show
Bug Best Practice introduced by
The property name does not exist on App\Models\User. Since you implemented __get, consider adding a @property annotation.
Loading history...
50
        ]);
51
    }
52
53
    /**
54
     * Creates contact by user collection and returns contact.
55
     *
56
     * @param Collection $users
57
     * @return Collection
58
     */
59 10
    public function setContacts(Collection $users): Collection
60
    {
61 10
        return $users->map(function ($user) use ($users) {
62
            return $users
63 10
                    ->filter(function ($item) use ($user) {
64 10
                        return ! $item->is($user);
65 10
                    })
66 10
                    ->map(function ($contact) use ($user) {
67 7
                        return $this->addContact($user, $contact);
68 10
                    });
69 10
        })
70 10
                ->flatten();
71
    }
72
73
    /**
74
     * Remove a contact.
75
     *
76
     * @param string $contact_id
77
     * @return Contact
78
     */
79 1
    public function removeContact(string $contact_id): Contact
80
    {
81 1
        $contact = Contact::find($contact_id);
82 1
        $contact->delete();
83
84 1
        return $contact;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $contact could return the type null which is incompatible with the type-hinted return App\Models\Contact. Consider adding an additional type-check to rule them out.
Loading history...
85
    }
86
}
87