|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Azine\EmailBundle\Services; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\Common\Persistence\ManagerRegistry; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Default implementation of the RecipientProviderInterface. |
|
9
|
|
|
*/ |
|
10
|
|
|
class AzineRecipientProvider implements RecipientProviderInterface |
|
11
|
|
|
{ |
|
12
|
|
|
/** @var ManagerRegistry */ |
|
13
|
|
|
private $managerRegistry; |
|
14
|
|
|
|
|
15
|
|
|
/** @var string your recipient class */ |
|
16
|
|
|
private $userClass; |
|
17
|
|
|
|
|
18
|
|
|
/** @var string the field name of the boolean field that indicate wether or not a newsletter should be sent to the recipient entity. */ |
|
19
|
|
|
private $newsletterField; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @param ManagerRegistry $managerRegistry |
|
23
|
|
|
* @param string $userClass |
|
24
|
|
|
* @param string $newsletterField |
|
25
|
|
|
*/ |
|
26
|
2 |
|
public function __construct(ManagerRegistry $managerRegistry, $userClass, $newsletterField) |
|
27
|
|
|
{ |
|
28
|
2 |
|
$this->managerRegistry = $managerRegistry; |
|
29
|
2 |
|
$this->userClass = $userClass; |
|
30
|
2 |
|
$this->newsletterField = $newsletterField; |
|
31
|
2 |
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* (non-PHPdoc). |
|
35
|
|
|
* |
|
36
|
|
|
* @see Azine\EmailBundle\Services.RecipientProviderInterface::getRecipient() |
|
37
|
|
|
*/ |
|
38
|
1 |
|
public function getRecipient($id) |
|
39
|
|
|
{ |
|
40
|
1 |
|
return $this->managerRegistry->getManager()->getRepository($this->userClass)->find($id); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* (non-PHPdoc). |
|
45
|
|
|
* |
|
46
|
|
|
* @see Azine\EmailBundle\Services.RecipientProviderInterface::getNewsletterRecipientIDs() |
|
47
|
|
|
*/ |
|
48
|
1 |
|
public function getNewsletterRecipientIDs() |
|
49
|
|
|
{ |
|
50
|
1 |
|
$qb = $this->managerRegistry->getManager()->createQueryBuilder() |
|
51
|
1 |
|
->select('n.id') |
|
52
|
1 |
|
->from($this->userClass, 'n') |
|
53
|
1 |
|
->where('n.'.$this->newsletterField.' = true') |
|
54
|
1 |
|
->andWhere('n.enabled = 1') // exclude inactive users |
|
55
|
|
|
; |
|
56
|
1 |
|
$results = $qb->getQuery()->execute(); |
|
57
|
1 |
|
$ids = array(); |
|
58
|
1 |
|
foreach ($results as $next) { |
|
59
|
1 |
|
$ids[] = $next['id']; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
1 |
|
return $ids; |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|