Completed
Push — 3.x-dev-kit ( 0a4698 )
by
unknown
13:04
created

ErroneousMessagesSelector::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
3
/*
4
 * This file is part of the Sonata Project package.
5
 *
6
 * (c) Thomas Rabaix <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sonata\NotificationBundle\Selector;
13
14
use Doctrine\Common\Persistence\ManagerRegistry;
15
use Sonata\NotificationBundle\Model\MessageInterface;
16
17
class ErroneousMessagesSelector
18
{
19
    /**
20
     * @var string
21
     */
22
    protected $class;
23
24
    /**
25
     * @var ManagerRegistry
26
     */
27
    protected $registry;
28
29
    /**
30
     * @param ManagerRegistry $registry
31
     * @param string          $class
32
     */
33
    public function __construct(ManagerRegistry $registry, $class)
34
    {
35
        $this->registry = $registry;
36
        $this->class = $class;
37
    }
38
39
    /**
40
     * Retrieve messages with given type(s) and restrict to max attempts count.
41
     *
42
     * @param array $types
43
     * @param int   $maxAttempts
44
     *
45
     * @return array
46
     */
47
    public function getMessages(array $types, $maxAttempts = 5)
48
    {
49
        $query = $this->registry->getManagerForClass($this->class)->getRepository($this->class)
50
            ->createQueryBuilder('m')
51
            ->where('m.state = :erroneousState')
52
            ->andWhere('m.restartCount < :maxAttempts');
53
54
        $parameters = array(
55
            'erroneousState' => MessageInterface::STATE_ERROR,
56
            'maxAttempts' => $maxAttempts,
57
        );
58
59
        if (count($types) > 0) {
60
            $query->andWhere('m.type IN (:types)');
61
            $parameters['types'] = $types;
62
        }
63
64
        $query->setParameters($parameters);
65
66
        return $query->getQuery()->execute();
67
    }
68
}
69