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
|
|
|
|