FileTransport   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 96.15%

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 3
dl 0
loc 89
ccs 25
cts 26
cp 0.9615
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 3
A create() 0 4 1
A __toString() 0 4 1
A getLastModifiedDate() 0 4 1
A getSize() 0 4 1
A getFilename() 0 4 1
A getDefaultDestination() 0 10 3
A doFetch() 0 9 2
1
<?php
2
3
namespace TreeHouse\Feeder\Transport;
4
5
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
6
use TreeHouse\Feeder\Exception\TransportException;
7
8
class FileTransport extends AbstractTransport
9
{
10
    /**
11
     * @inheritdoc
12
     */
13 66
    public function __construct(Connection $conn, $destination = null, EventDispatcherInterface $dispatcher = null)
14
    {
15 66
        parent::__construct($conn, $destination, $dispatcher);
16
17 66
        if (!isset($this->connection['file'])) {
18 2
            throw new \InvalidArgumentException('The "file" key is required in the Connection object');
19
        }
20
21 64
        if (!is_readable($this->connection['file'])) {
22 4
            throw new TransportException(sprintf('Not readable: %s', $this->connection['file']));
23
        }
24 60
    }
25
26
    /**
27
     * Factory method.
28
     *
29
     * @param string $file
30
     *
31
     * @return FileTransport
32
     */
33 62
    public static function create($file)
34
    {
35 62
        return new self(new Connection(['file' => $file]));
36
    }
37
38
    /**
39
     * @inheritdoc
40
     */
41 12
    public function __toString()
42
    {
43 12
        return (string) $this->connection['file'];
44
    }
45
46
    /**
47
     * @inheritdoc
48
     */
49 40
    public function getLastModifiedDate()
50
    {
51 40
        return new \DateTime('@' . filemtime($this->connection['file']));
52
    }
53
54
    /**
55
     * @return int
56
     */
57 2
    public function getSize()
58
    {
59 2
        return filesize($this->connection['file']);
60
    }
61
62
    /**
63
     * @return string
64
     */
65 4
    public function getFilename()
66
    {
67 4
        return basename($this->connection['file']);
68
    }
69
70
    /**
71
     * @return string
72
     */
73 46
    public function getDefaultDestination()
74
    {
75
        // if the destination dir is not set or the same, use the original file
76 46
        if (!$this->getDestinationDir() || (dirname($this->connection['file']) === $this->getDestinationDir())) {
77 32
            return $this->connection['file'];
78
        }
79
80
        // make sure the file is copied to the specified destination
81 16
        return sprintf('%s/%s', rtrim($this->getDestinationDir(), '/'), basename($this->connection['file']));
82
    }
83
84
    /**
85
     * @inheritdoc
86
     */
87 8
    protected function doFetch($destination)
88
    {
89
        // the destination may be the same as the source
90 8
        if (realpath($this->connection['file']) === realpath($destination)) {
91
            return;
92
        }
93
94 8
        copy($this->connection['file'], $destination);
95 8
    }
96
}
97