SpreadsheetLoader::addLoader()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace Akeneo\Component\SpreadsheetParser;
4
5
use InvalidArgumentException;
6
7
/**
8
 * Format agnostic spreadsheet loader
9
 *
10
 * @author    Antoine Guigan <[email protected]>
11
 * @copyright 2014 Akeneo SAS (http://www.akeneo.com)
12
 * @license   http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
13
 */
14
class SpreadsheetLoader implements SpreadsheetLoaderInterface
15
{
16
    /**
17
     * @var SpreadsheetLoaderInterface[]
18
     */
19
    protected $loaders = [];
20
21
    /**
22
     * Opens a spreadsheet
23
     *
24
     * @param string      $path
25
     * @param string|null $type
26
     *
27
     * @return SpreadsheetInterface
28
     *
29
     * @throws InvalidArgumentException
30
     */
31
    public function open($path, $type = null)
32
    {
33
        $type = $type ?: $this->getType($path);
34
        if (!isset($this->loaders[$type])) {
35
            throw new InvalidArgumentException(sprintf('No loader for type %s', $type));
36
        }
37
38
        return $this->loaders[$type]->open($path);
39
    }
40
41
    /**
42
     * Addds a loader for a specified type
43
     *
44
     * @param string                     $type
45
     * @param SpreadsheetLoaderInterface $loader
46
     *
47
     * @return SpreadsheetLoader
48
     */
49
    public function addLoader($type, SpreadsheetLoaderInterface $loader)
50
    {
51
        $this->loaders[$type] = $loader;
52
53
        return $this;
54
    }
55
56
    /**
57
     * Returns the type for a path
58
     *
59
     * @param string $path
60
     *
61
     * @return string
62
     */
63
    protected function getType($path)
64
    {
65
        return strtolower(pathinfo($path, PATHINFO_EXTENSION));
66
    }
67
}
68