Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
7 / 7
CRAP
100.00% covered (success)
100.00%
1 / 1
CreatesNewPublicationPage
100.00% covered (success)
100.00%
22 / 22
100.00% covered (success)
100.00%
7 / 7
9
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 handleCreate
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getFilename
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getCanonicalValue
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
3
 createFrontMatter
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 normalizeData
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getFieldFromCollection
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 RuntimeException;
8use Illuminate\Support\Carbon;
9use Symfony\Component\Yaml\Yaml;
10use Illuminate\Support\Collection;
11use Hyde\Publications\Models\PublicationType;
12use Hyde\Publications\Models\PublicationFieldValue;
13use Hyde\Publications\Concerns\PublicationFieldTypes;
14use Hyde\Framework\Actions\ConvertsArrayToFrontMatter;
15
16use function substr;
17
18/**
19 * Scaffold a publication file.
20 *
21 * @see \Hyde\Publications\Commands\MakePublicationCommand
22 * @see \Hyde\Publications\Testing\Feature\CreatesNewPublicationPageTest
23 */
24class CreatesNewPublicationPage extends CreateAction
25{
26    protected bool $force = false;
27    protected Collection $fieldData;
28    protected PublicationType $publicationType;
29
30    /**
31     * @param  \Hyde\Publications\Models\PublicationType  $publicationType
32     * @param  \Illuminate\Support\Collection<string, \Hyde\Publications\Models\PublicationFieldValue>  $fieldData
33     * @param  bool  $force
34     */
35    public function __construct(PublicationType $publicationType, Collection $fieldData, bool $force = false)
36    {
37        $fieldData->prepend(new PublicationFieldValue(PublicationFieldTypes::Datetime, (string) Carbon::now()), '__createdAt');
38
39        $this->publicationType = $publicationType;
40        $this->fieldData = $fieldData;
41        $this->force = $force;
42        $this->outputPath = "{$this->publicationType->getDirectory()}/{$this->getFilename()}.md";
43    }
44
45    protected function handleCreate(): void
46    {
47        $this->save("{$this->createFrontMatter()}\n## Write something awesome.\n\n");
48    }
49
50    protected function getFilename(): string
51    {
52        return $this->formatStringForStorage(substr($this->getCanonicalValue(), 0, 64));
53    }
54
55    protected function getCanonicalValue(): string
56    {
57        $canonicalFieldName = $this->publicationType->canonicalField;
58        if ($canonicalFieldName === '__createdAt') {
59            return $this->getFieldFromCollection('__createdAt')->getValue()->format('Y-m-d H:i:s');
60        }
61
62        if ($this->fieldData->has($canonicalFieldName)) {
63            $field = $this->getFieldFromCollection($canonicalFieldName);
64
65            return (string) $field->getValue();
66        } else {
67            return throw new RuntimeException("Could not find field value for '$canonicalFieldName' which is required as it's the type's canonical field", 404);
68        }
69    }
70
71    protected function createFrontMatter(): string
72    {
73        return (new ConvertsArrayToFrontMatter())->execute(
74            $this->normalizeData($this->fieldData),
75            flags: YAML::DUMP_MULTI_LINE_LITERAL_BLOCK
76        );
77    }
78
79    /**
80     * @param  Collection<string, PublicationFieldValue>  $data
81     * @return array<string, mixed>
82     */
83    protected function normalizeData(Collection $data): array
84    {
85        return $data->mapWithKeys(function (PublicationFieldValue $field, string $key): array {
86            return [$key => $field->getValue()];
87        })->toArray();
88    }
89
90    protected function getFieldFromCollection(string $key): PublicationFieldValue
91    {
92        return $this->fieldData->get($key);
93    }
94}