Completed
Push — dev ( 269014...5eca8b )
by Андрей
03:28
created

EntryNameResolverChain   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
c 1
b 0
f 0
lcom 1
cbo 2
dl 0
loc 97
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A count() 0 4 1
A attach() 0 12 1
A prependResolver() 0 21 2
A resolveEntryNameByContext() 0 3 1
1
<?php
2
/**
3
 * @link    https://github.com/nnx-framework/entry-name-resolver
4
 * @author  Malofeykin Andrey  <[email protected]>
5
 */
6
namespace Nnx\EntryNameResolver;
7
8
use Countable;
9
use Zend\Stdlib\PriorityQueue;
10
11
/**
12
 * Class EntryNameResolver
13
 *
14
 * @package Nnx\EntryNameResolver\EntryNameResolver
15
 */
16
class EntryNameResolverChain implements Countable, EntryNameResolverInterface
17
{
18
    /**
19
     * Приоритет по умолчанию для резолвера, в момент его добавления  в цепочку
20
     */
21
    const DEFAULT_PRIORITY = 1;
22
23
    /**
24
     * Цепочка резолверов
25
     *
26
     * @var PriorityQueue
27
     */
28
    protected $resolvers;
29
30
    /**
31
     * EntryNameResolver constructor.
32
     */
33
    public function __construct()
34
    {
35
        $this->resolvers = new PriorityQueue();
36
    }
37
38
    /**
39
     * Возвращает колличество добавленных резолверов
40
     *
41
     * @return int
42
     */
43
    public function count()
44
    {
45
        return count($this->resolvers);
46
    }
47
48
49
    /**
50
     * Добавляет резолвер
51
     *
52
     * @param EntryNameResolverInterface $resolver
53
     * @param int                        $priority
54
     *
55
     * @return $this
56
     */
57
    public function attach(EntryNameResolverInterface $resolver, $priority = self::DEFAULT_PRIORITY)
58
    {
59
        $this->resolvers->insert(
60
            [
61
                'instance' => $resolver,
62
63
            ],
64
            $priority
65
        );
66
67
        return $this;
68
    }
69
70
71
    /**
72
     * Добавляет резолвер в начало цепочки
73
     *
74
     * @param EntryNameResolverInterface $resolver
75
     *
76
     * @return $this
77
     *
78
     */
79
    public function prependResolver(EntryNameResolverInterface $resolver)
80
    {
81
        $priority = self::DEFAULT_PRIORITY;
82
83
        if (!$this->resolvers->isEmpty()) {
84
            $queue = $this->resolvers->getIterator();
85
            $queue->setExtractFlags(PriorityQueue::EXTR_PRIORITY);
86
            $extractedNode = $queue->extract();
87
            $priority = $extractedNode[0] + 1;
88
        }
89
90
        $this->resolvers->insert(
91
            [
92
                'instance' => $resolver,
93
94
            ],
95
            $priority
96
        );
97
98
        return $this;
99
    }
100
101
    /**
102
     * @inheritdoc
103
     *
104
     * @param      $entryName
105
     * @param null $context
106
     *
107
     * @return mixed
108
     */
109
    public function resolveEntryNameByContext($entryName, $context = null)
110
    {
111
    }
112
}
113