GenericTeleporter::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 3
1
<?php
2
3
namespace Alchemy\Zippy\Resource\Teleporter;
4
5
use Alchemy\Zippy\Exception\InvalidArgumentException;
6
use Alchemy\Zippy\Exception\IOException;
7
use Alchemy\Zippy\Resource\Resource as ZippyResource;
8
use Alchemy\Zippy\Resource\ResourceLocator;
9
use Alchemy\Zippy\Resource\ResourceReaderFactory;
10
use Alchemy\Zippy\Resource\ResourceWriter;
11
12
class GenericTeleporter implements TeleporterInterface
13
{
14
    /**
15
     * @var ResourceReaderFactory
16
     */
17
    private $readerFactory;
18
19
    /**
20
     * @var ResourceWriter
21
     */
22
    private $resourceWriter;
23
24
    /**
25
     * @var ResourceLocator
26
     */
27
    private $resourceLocator;
28
29
    /**
30
     * @param ResourceReaderFactory $readerFactory
31
     * @param ResourceWriter        $resourceWriter
32
     * @param ResourceLocator       $resourceLocator
33
     */
34
    public function __construct(
35
        ResourceReaderFactory $readerFactory,
36
        ResourceWriter $resourceWriter,
37
        ResourceLocator $resourceLocator = null
38
    ) {
39
        $this->readerFactory = $readerFactory;
40
        $this->resourceWriter = $resourceWriter;
41
        $this->resourceLocator = $resourceLocator ?: new ResourceLocator();
42
    }
43
44
    /**
45
     * Teleports a file from a destination to an other
46
     *
47
     * @param ZippyResource $resource A Resource
48
     * @param string        $context  The current context
49
     *
50
     * @throws IOException when file could not be written on local
51
     * @throws InvalidArgumentException when path to file is not valid
52
     */
53
    public function teleport(ZippyResource $resource, $context)
54
    {
55
        $reader = $this->readerFactory->getReader($resource, $context);
56
        $target = $this->resourceLocator->mapResourcePath($resource, $context);
57
58
        $this->resourceWriter->writeFromReader($reader, $target);
59
    }
60
}
61