ThirdPartyAwareTrait   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 2 Features 0
Metric Value
wmc 10
c 2
b 2
f 0
lcom 1
cbo 2
dl 0
loc 75
ccs 0
cts 23
cp 0
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A initThirdPartyCredentials() 0 4 1
A getThirdPartyCredentials() 0 4 1
A setThirdPartyCredentials() 0 7 2
A addThirdParty() 0 8 2
A removeThirdParty() 0 4 1
A findThirdPartyByProvider() 0 10 3
1
<?php
2
/**
3
 * @author Stefano Torresi (http://stefanotorresi.it)
4
 * @license See the file LICENSE.txt for copying permission.
5
 * ************************************************
6
 */
7
8
namespace Thorr\OAuth2\Entity;
9
10
use Doctrine\Common\Collections;
11
12
/**
13
 * Don't forget to initialize $thirdPartyUsers property in the constructor
14
 */
15
trait ThirdPartyAwareTrait
16
{
17
    /**
18
     * @var Collections\Collection
19
     */
20
    protected $thirdPartyCredentials;
21
22
    /**
23
     * @return array
24
     */
25
    public function getThirdPartyCredentials()
26
    {
27
        return $this->thirdPartyCredentials->toArray();
28
    }
29
30
    /**
31
     * @param array|Collections\Collection $thirdPartyUsers
32
     */
33
    public function setThirdPartyCredentials($thirdPartyUsers)
34
    {
35
        if (! $thirdPartyUsers instanceof Collections\Collection) {
36
            $thirdPartyUsers = new Collections\ArrayCollection($thirdPartyUsers);
37
        }
38
        $this->thirdPartyCredentials = $thirdPartyUsers;
39
    }
40
41
    /**
42
     * @param ThirdParty $thirdParty
43
     *
44
     * @return bool
45
     */
46
    public function addThirdParty(ThirdParty $thirdParty)
47
    {
48
        if ($this->thirdPartyCredentials->contains($thirdParty)) {
49
            return false;
50
        }
51
52
        return $this->thirdPartyCredentials->add($thirdParty);
53
    }
54
55
    /**
56
     * @param ThirdParty $thirdParty
57
     *
58
     * @return bool
59
     */
60
    public function removeThirdParty(ThirdParty $thirdParty)
61
    {
62
        return (bool) $this->thirdPartyCredentials->remove($thirdParty);
63
    }
64
65
    /**
66
     * @param string $provider
67
     *
68
     * @return ThirdParty|null
69
     */
70
    public function findThirdPartyByProvider($provider)
71
    {
72
        foreach ($this->thirdPartyCredentials as $thirdParty) { /* @var ThirdParty $thirdParty */
73
            if ($thirdParty->getProvider() === $provider) {
74
                return $thirdParty;
75
            };
76
        }
77
78
        return;
79
    }
80
81
    /**
82
     * initializes an empty array collection
83
     * call this in the constructor of each class implementing this trait
84
     */
85
    private function initThirdPartyCredentials()
86
    {
87
        $this->thirdPartyCredentials = new Collections\ArrayCollection();
88
    }
89
}
90