ErroneousMessagesSelector   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 1
dl 0
loc 50
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getMessages() 0 21 2
A __construct() 0 5 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\NotificationBundle\Selector;
15
16
use Doctrine\Common\Persistence\ManagerRegistry;
17
use Sonata\NotificationBundle\Model\MessageInterface;
18
19
class ErroneousMessagesSelector
20
{
21
    /**
22
     * @var string
23
     */
24
    protected $class;
25
26
    /**
27
     * @var ManagerRegistry
28
     */
29
    protected $registry;
30
31
    /**
32
     * @param string $class
33
     */
34
    public function __construct(ManagerRegistry $registry, $class)
35
    {
36
        $this->registry = $registry;
37
        $this->class = $class;
38
    }
39
40
    /**
41
     * Retrieve messages with given type(s) and restrict to max attempts count.
42
     *
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 = [
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