CreatesNewPublicationType::createDetailTemplate()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Publications\Actions;
6
7
use Hyde\Hyde;
8
use Illuminate\Contracts\Support\Arrayable;
9
use Hyde\Publications\Models\PublicationType;
10
11
use 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
 */
19
class 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"));
0 ignored issues
show
Bug introduced by
The method vendorPath() does not exist on Hyde\Hyde. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

64
        copy(Hyde::/** @scrutinizer ignore-call */ vendorPath("/../publications/resources/views/$viewName.blade.php"), Hyde::path("$this->directoryName/$filename.blade.php"));
Loading history...
Bug introduced by
The method path() does not exist on Hyde\Hyde. Since you implemented __callStatic, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

64
        copy(Hyde::vendorPath("/../publications/resources/views/$viewName.blade.php"), Hyde::/** @scrutinizer ignore-call */ path("$this->directoryName/$filename.blade.php"));
Loading history...
65
    }
66
67
    protected function usesPagination(): bool
68
    {
69
        return $this->pageSize > 0;
70
    }
71
}
72