TrackerFactory::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
/**
4
 * Tack Tracker - A library for tracking long-running task progress
5
 *
6
 * @license http://opensource.org/licenses/MIT
7
 * @link https://github.com/caseyamcl/tasktracker
8
 * @version 2.0
9
 * @package caseyamcl/tasktracker
10
 * @author Casey McLaughlin <[email protected]>
11
 *
12
 * For the full copyright and license information, please view the LICENSE
13
 * file that was distributed with this source code.
14
 *
15
 * ------------------------------------------------------------------
16
 */
17
18
namespace TaskTracker;
19
20
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
21
22
/**
23
 * Tracker Factory Service Class
24
 *
25
 * Provides a service library for building many Tracker objects
26
 * with the same set of subscribers
27
 *
28
 * @author Casey McLaughlin <[email protected]>
29
 */
30
class TrackerFactory
31
{
32
    /**
33
     * @var array|EventSubscriberInterface[]
34
     */
35
    private $defaultSubscribers;
36
37
    /**
38
     * Constructor
39
     *
40
     * @param EventSubscriberInterface[] $defaultSubscribers
41
     */
42
    public function __construct(array $defaultSubscribers = [])
43
    {
44
        $this->defaultSubscribers = $defaultSubscribers;
45
    }
46
47
    /**
48
     * Build a new Tracker instance
49
     *
50
     * If $extraSubscribers is not empty, those subscribers will be added
51
     * to the Tracker in addition to the defaults.
52
     *
53
     * @param int                               $numItems          The total number of items (or -1 for unknown)
54
     * @param array|EventSubscriberInterface[]  $extraSubscribers  Optionally specify extra listeners for this Tracker instance
55
     * @return Tracker
56
     */
57
    public function buildTracker($numItems = Tracker::UNKNOWN, array $extraSubscribers = [])
58
    {
59
        $tracker = new Tracker($numItems);
60
61
        foreach (array_merge($this->defaultSubscribers, $extraSubscribers) as $listener) {
62
            $tracker->getDispatcher()->addSubscriber($listener);
63
        }
64
65
        return $tracker;
66
    }
67
}
68