Completed
Push — 15.x ( b45baa...4ca9b4 )
by Tim
02:13
created

AbstractFinderRepository::addFinder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
/**
4
 * TechDivision\Import\Repositories\AbstractFinderRepository
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Tim Wagner <[email protected]>
15
 * @copyright 2016 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/techdivision/import
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Repositories;
22
23
use TechDivision\Import\Connection\ConnectionInterface;
24
use TechDivision\Import\Repositories\Finders\FinderInterface;
25
use TechDivision\Import\Repositories\Finders\FinderFactoryInterface;
26
27
/**
28
 * Abstract repository implementation with finder support.
29
 *
30
 * @author    Tim Wagner <[email protected]>
31
 * @copyright 2016 TechDivision GmbH <[email protected]>
32
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
33
 * @link      https://github.com/techdivision/import
34
 * @link      http://www.techdivision.com
35
 */
36
abstract class AbstractFinderRepository extends AbstractRepository implements FinderAwareRepositoryInterface
37
{
38
39
    /**
40
     * The finder factory.
41
     *
42
     * @var \TechDivision\Import\Repositories\Finders\FinderFactoryInterface
43
     */
44
    protected $finderFactory;
45
46
    /**
47
     * The array with the initialized finders.
48
     *
49
     * @var array
50
     */
51
    protected $finders = array();
52
53
    /**
54
     * Initialize the repository with the passed connection and utility class name.
55
     * .
56
     * @param \TechDivision\Import\Connection\ConnectionInterface               $connection             The connection instance
57
     * @param \TechDivision\Import\Repositories\SqlStatementRepositoryInterface $sqlStatementRepository The SQL repository instance
58
     * @param \TechDivision\Import\Repositories\Finders\FinderFactoryInterface  $finderFactory          The finder factory instance
59
     */
60
    public function __construct(
61
        ConnectionInterface $connection,
62
        SqlStatementRepositoryInterface $sqlStatementRepository,
63
        FinderFactoryInterface $finderFactory
64
    ) {
65
66
        // set the finder factory
67
        $this->finderFactory = $finderFactory;
68
69
        // pass the connection the SQL statement repository to the parent class
70
        parent::__construct($connection, $sqlStatementRepository);
71
    }
72
73
    /**
74
     * Add the initialize finder to the repository.
75
     *
76
     * @param \TechDivision\Import\Repositories\Finders\FinderInterface $finder The finder instance to add
77
     *
78
     * @return void
79
     */
80
    public function addFinder(FinderInterface $finder)
81
    {
82
        $this->finders[$finder->getKey()] = $finder;
83
    }
84
85
    /**
86
     * Return's the finder instance with the passed key.
87
     *
88
     * @param string $key The key of the finder to return
89
     *
90
     * @return \TechDivision\Import\Repositories\Finders\FinderInterface The finder instance
91
     * @throws \InvalidArgumentException Is thrown if the finder with the passed key is not available
92
     */
93
    public function getFinder($key)
94
    {
95
96
        // query whether or not the finder is available
97
        if (isset($this->finders[$key])) {
98
            return $this->finders[$key];
99
        }
100
101
        // throw an exception, if not
102
        throw new \InvalidArgumentException(sprintf('Finder "%s" has not ebeen registered', $key));
103
    }
104
}
105