1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Jobtech\LaravelChunky\Strategies; |
4
|
|
|
|
5
|
|
|
use Illuminate\Container\Container; |
6
|
|
|
use Illuminate\Support\Traits\ForwardsCalls; |
7
|
|
|
use Jobtech\LaravelChunky\Contracts\ChunksManager; |
8
|
|
|
use Jobtech\LaravelChunky\Exceptions\StrategyException; |
9
|
|
|
use Jobtech\LaravelChunky\Strategies\Contracts\MergeStrategy as MergeStrategyContract; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @mixin ChunksManager |
13
|
|
|
*/ |
14
|
|
|
abstract class MergeStrategy implements MergeStrategyContract |
15
|
|
|
{ |
16
|
|
|
use ForwardsCalls; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
protected $folder; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var \Jobtech\LaravelChunky\Contracts\ChunksManager |
25
|
|
|
*/ |
26
|
|
|
protected $manager; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
protected $destination; |
32
|
|
|
|
33
|
|
|
public function __construct($manager = null) |
34
|
|
|
{ |
35
|
|
|
$this->manager = $manager ?: Container::getInstance() |
36
|
|
|
->make('chunky'); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Retrieve chunks and map the paths into an array. |
41
|
|
|
* |
42
|
|
|
* @return array |
43
|
|
|
*/ |
44
|
|
|
protected function mapChunksToArray(): array |
45
|
|
|
{ |
46
|
|
|
return $this->chunks( |
|
|
|
|
47
|
|
|
$this->folder |
48
|
|
|
)->map(function ($chunk) { |
49
|
|
|
return $chunk['path']; |
50
|
|
|
})->toArray(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritdoc} |
55
|
|
|
*/ |
56
|
|
|
public function manager($manager = null): ChunksManager |
57
|
|
|
{ |
58
|
|
|
if ($manager instanceof ChunksManager) { |
59
|
|
|
$this->manager = $manager; |
60
|
|
|
} elseif ($this->manager === null) { |
61
|
|
|
throw new StrategyException('Manager cannot be empty'); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return $this->manager; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritdoc} |
69
|
|
|
*/ |
70
|
|
|
public function chunksFolder($folder = null): string |
71
|
|
|
{ |
72
|
|
|
if (is_string($folder) && $this->chunksFolderExists($folder)) { |
|
|
|
|
73
|
|
|
$this->folder = $folder; |
74
|
|
|
} elseif (empty($this->folder) || ! $this->chunksFolderExists($this->folder)) { |
75
|
|
|
throw new StrategyException('Chunks folder cannot be empty'); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return $this->folder; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* {@inheritdoc} |
83
|
|
|
*/ |
84
|
|
|
public function destination($destination = null): string |
85
|
|
|
{ |
86
|
|
|
if (is_string($destination)) { |
87
|
|
|
$this->destination = $destination; |
88
|
|
|
} elseif (empty($this->destination)) { |
89
|
|
|
throw new StrategyException('Destination path cannot be empty'); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return $this->destination; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function __call($method, $parameters) |
96
|
|
|
{ |
97
|
|
|
if (! method_exists($this, $method)) { |
98
|
|
|
return $this->forwardCallTo($this->manager, $method, $parameters); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return $this->{$method}(...$parameters); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Create a new instance for the strategy. |
106
|
|
|
* |
107
|
|
|
* @param \Jobtech\LaravelChunky\Contracts\ChunksManager|null $manager |
108
|
|
|
* |
109
|
|
|
* @return static |
110
|
|
|
*/ |
111
|
|
|
public static function newInstance($manager = null) |
112
|
|
|
{ |
113
|
|
|
return new static($manager); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|