Completed
Pull Request — master (#161)
by Alexander
04:42
created

LocalTeleporter   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 6
dl 0
loc 59
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A teleport() 0 21 5
A create() 0 4 1
1
<?php
2
3
/*
4
 * This file is part of Zippy.
5
 *
6
 * (c) Alchemy <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Alchemy\Zippy\Resource\Teleporter;
13
14
use Alchemy\Zippy\Exception\InvalidArgumentException;
15
use Alchemy\Zippy\Exception\IOException;
16
use Alchemy\Zippy\Resource\Resource as ZippyResource;
17
use Alchemy\Zippy\Resource\ResourceLocator;
18
use Symfony\Component\Filesystem\Exception\IOException as SfIOException;
19
use Symfony\Component\Filesystem\Filesystem;
20
21
/**
22
 * This class transports an object using the local filesystem
23
 */
24
class LocalTeleporter extends AbstractTeleporter
0 ignored issues
show
Deprecated Code introduced by
The class Alchemy\Zippy\Resource\T...rter\AbstractTeleporter has been deprecated with message: Typehint against TeleporterInterface instead and use GenericTeleporter with custom reader/writers instead. This class will be removed in v0.5.x

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
25
{
26
    /**
27
     * @var Filesystem
28
     */
29
    private $filesystem;
30
31
    /**
32
     * @var ResourceLocator
33
     */
34
    private $resourceLocator;
35
36
    /**
37
     * Constructor
38
     *
39
     * @param Filesystem $filesystem
40
     */
41
    public function __construct(Filesystem $filesystem)
42
    {
43
        $this->filesystem = $filesystem;
44
        $this->resourceLocator = new ResourceLocator();
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function teleport(ZippyResource $resource, $context)
51
    {
52
        $target = $this->resourceLocator->mapResourcePath($resource, $context);
53
        $path = $resource->getOriginal();
54
55
        if (!file_exists($path)) {
56
            throw new InvalidArgumentException(sprintf('Invalid path %s', $path));
57
        }
58
59
        try {
60
            if (is_file($path)) {
61
                $this->filesystem->copy($path, $target);
62
            } elseif (is_dir($path)) {
63
                $this->filesystem->mirror($path, $target);
64
            } else {
65
                throw new InvalidArgumentException(sprintf('Invalid file or directory %s', $path));
66
            }
67
        } catch (SfIOException $e) {
68
            throw new IOException(sprintf('Could not write %s', $target), $e->getCode(), $e);
69
        }
70
    }
71
72
    /**
73
     * Creates the LocalTeleporter
74
     *
75
     * @return LocalTeleporter
76
     * @deprecated This method will be removed in a future release (0.5.x)
77
     */
78
    public static function create()
79
    {
80
        return new static(new Filesystem());
81
    }
82
}
83