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
|
|
|
|