Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
6 / 6
CRAP
100.00% covered (success)
100.00%
1 / 1
CreatesNewPublicationType
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
6 / 6
7
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 handleCreate
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
1 / 1
1
 createDetailTemplate
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 createListTemplate
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 publishPublicationFile
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 usesPagination
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace Hyde\Publications\Actions;
6
7use Hyde\Hyde;
8use Illuminate\Contracts\Support\Arrayable;
9use Hyde\Publications\Models\PublicationType;
10
11use function copy;
12
13/**
14 * Scaffold a new publication type schema.
15 *
16 * @see \Hyde\Publications\Commands\MakePublicationCommand
17 * @see \Hyde\Publications\Testing\Feature\CreatesNewPublicationTypeTest
18 */
19class CreatesNewPublicationType extends CreateAction
20{
21    protected string $directoryName;
22
23    public function __construct(
24        protected string $name,
25        protected Arrayable $fields,
26        protected ?string $canonicalField = null,
27        protected ?string $sortField = null,
28        protected ?bool $sortAscending = null,
29        protected ?int $pageSize = null,
30    ) {
31        $this->directoryName = $this->formatStringForStorage($this->name);
32        $this->outputPath = "$this->directoryName/schema.json";
33    }
34
35    protected function handleCreate(): void
36    {
37        (new PublicationType(
38            $this->name,
39            $this->canonicalField ?? '__createdAt',
40            'detail.blade.php',
41            'list.blade.php',
42            $this->sortField ?? '__createdAt',
43            $this->sortAscending ?? true,
44            $this->pageSize ?? 0,
45            $this->fields->toArray()
46        ))->save($this->outputPath);
47
48        $this->createDetailTemplate();
49        $this->createListTemplate();
50    }
51
52    protected function createDetailTemplate(): void
53    {
54        $this->publishPublicationFile('detail', 'detail');
55    }
56
57    protected function createListTemplate(): void
58    {
59        $this->publishPublicationFile('list', $this->usesPagination() ? 'paginated_list' : 'list');
60    }
61
62    protected function publishPublicationFile(string $filename, string $viewName): void
63    {
64        copy(Hyde::vendorPath("/../publications/resources/views/$viewName.blade.php"), Hyde::path("$this->directoryName/$filename.blade.php"));
65    }
66
67    protected function usesPagination(): bool
68    {
69        return $this->pageSize > 0;
70    }
71}