1 | <?php |
||
2 | |||
3 | namespace Astrotomic\Stancy\Solutions; |
||
4 | |||
5 | use Facade\IgnitionContracts\RunnableSolution; |
||
6 | use Illuminate\Contracts\Filesystem\Filesystem; |
||
7 | use Illuminate\Support\Str; |
||
8 | use ReflectionClass; |
||
9 | use Spatie\Sheets\Facades\Sheets; |
||
10 | use Spatie\Sheets\Repositories\FilesystemRepository; |
||
11 | |||
12 | class AddSheetToCollectionSolution implements RunnableSolution |
||
13 | { |
||
14 | /** @var string */ |
||
15 | protected $collection; |
||
16 | |||
17 | /** @var string */ |
||
18 | protected $sheet; |
||
19 | |||
20 | /** @var Filesystem|null */ |
||
21 | protected $filesystem; |
||
22 | |||
23 | /** @var string|null */ |
||
24 | protected $extension; |
||
25 | |||
26 | 4 | public function __construct(string $collection, string $sheet) |
|
27 | { |
||
28 | 4 | $this->collection = $collection; |
|
29 | 4 | $this->sheet = $sheet; |
|
30 | |||
31 | 4 | $this->copyPropertiesFromFilesystemRepository(); |
|
32 | 4 | } |
|
33 | |||
34 | 1 | public function getSolutionTitle(): string |
|
35 | { |
||
36 | 1 | return 'The sheet is missing'; |
|
37 | } |
||
38 | |||
39 | 1 | public function getSolutionDescription(): string |
|
40 | { |
||
41 | 1 | return "Add sheet `{$this->sheet}` to collection `{$this->collection}`."; |
|
42 | } |
||
43 | |||
44 | 1 | public function getRunButtonText(): string |
|
45 | { |
||
46 | 1 | return 'Add sheet file'; |
|
47 | } |
||
48 | |||
49 | 1 | public function getSolutionActionDescription(): string |
|
50 | { |
||
51 | 1 | return 'Pressing the button below will try to add the sheet file to the collection filesystem.'; |
|
52 | } |
||
53 | |||
54 | 1 | public function getRunParameters(): array |
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
55 | { |
||
56 | return [ |
||
57 | 1 | 'sheet' => $this->sheet, |
|
58 | ]; |
||
59 | } |
||
60 | |||
61 | 1 | public function getDocumentationLinks(): array |
|
0 ignored issues
–
show
|
|||
62 | { |
||
63 | 1 | return []; |
|
64 | } |
||
65 | |||
66 | 3 | public function run(array $parameters = []): bool |
|
0 ignored issues
–
show
|
|||
67 | { |
||
68 | 3 | if ($this->filesystem === null) { |
|
69 | return false; // @codeCoverageIgnore |
||
70 | } |
||
71 | |||
72 | 3 | if ($this->extension === null) { |
|
73 | return false; // @codeCoverageIgnore |
||
74 | } |
||
75 | |||
76 | 3 | return $this->filesystem->put(Str::finish($parameters['sheet'] ?? $this->sheet, '.'.$this->extension), ''); |
|
77 | } |
||
78 | |||
79 | 4 | protected function copyPropertiesFromFilesystemRepository(): void |
|
80 | { |
||
81 | 4 | $repository = Sheets::collection($this->collection); |
|
82 | |||
83 | 4 | if (! $repository instanceof FilesystemRepository) { |
|
84 | return; // @codeCoverageIgnore |
||
85 | } |
||
86 | |||
87 | 4 | $repositoryReflection = new ReflectionClass($repository); |
|
88 | |||
89 | 4 | $filesystemProperty = $repositoryReflection->getProperty('filesystem'); |
|
90 | 4 | $filesystemProperty->setAccessible(true); |
|
91 | 4 | $this->filesystem = $filesystemProperty->getValue($repository); |
|
92 | |||
93 | 4 | $extensionProperty = $repositoryReflection->getProperty('extension'); |
|
94 | 4 | $extensionProperty->setAccessible(true); |
|
95 | 4 | $this->extension = $extensionProperty->getValue($repository); |
|
96 | 4 | } |
|
97 | } |
||
98 |