GenerateEntryModel::handle()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 33
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 24
nc 1
nop 3
dl 0
loc 33
rs 8.8571
c 0
b 0
f 0
1
<?php namespace Anomaly\Streams\Platform\Entry\Command;
2
3
use Anomaly\Streams\Platform\Support\Parser;
4
use Anomaly\Streams\Platform\Application\Application;
5
use Anomaly\Streams\Platform\Entry\Parser\EntryClassParser;
6
use Anomaly\Streams\Platform\Entry\Parser\EntryDatesParser;
7
use Anomaly\Streams\Platform\Entry\Parser\EntryRulesParser;
8
use Anomaly\Streams\Platform\Entry\Parser\EntryTableParser;
9
use Anomaly\Streams\Platform\Entry\Parser\EntryTitleParser;
10
use Anomaly\Streams\Platform\Entry\Parser\EntryStreamParser;
11
use Anomaly\Streams\Platform\Stream\Contract\StreamInterface;
12
use Anomaly\Streams\Platform\Entry\Parser\EntryRelationsParser;
13
use Anomaly\Streams\Platform\Entry\Parser\EntryNamespaceParser;
14
use Anomaly\Streams\Platform\Entry\Parser\EntryTrashableParser;
15
use Anomaly\Streams\Platform\Entry\Parser\EntrySearchableParser;
16
use Anomaly\Streams\Platform\Entry\Parser\EntryFieldSlugsParser;
17
use Anomaly\Streams\Platform\Entry\Parser\EntryRelationshipsParser;
18
use Anomaly\Streams\Platform\Entry\Parser\EntryTranslationModelParser;
19
use Anomaly\Streams\Platform\Entry\Parser\EntryTranslatedAttributesParser;
20
use Anomaly\Streams\Platform\Entry\Parser\EntryTranslationForeignKeyParser;
21
use Illuminate\Filesystem\Filesystem;
22
23
class GenerateEntryModel
24
{
25
26
    /**
27
     * The stream interface.
28
     *
29
     * @var StreamInterface
30
     */
31
    protected $stream;
32
33
    /**
34
     * Create a new GenerateEntryModel instance.
35
     *
36
     * @param StreamInterface $stream
37
     */
38
    public function __construct(StreamInterface $stream)
39
    {
40
        $this->stream = $stream;
41
    }
42
43
    /**
44
     * Handle the command.
45
     *
46
     * @param Filesystem  $files
47
     * @param Parser      $parser
48
     * @param Application $application
49
     */
50
    public function handle(Filesystem $files, Parser $parser, Application $application)
51
    {
52
        $data = [
53
            'class'                   => (new EntryClassParser())->parse($this->stream),
54
            'title'                   => (new EntryTitleParser())->parse($this->stream),
55
            'table'                   => (new EntryTableParser())->parse($this->stream),
56
            'rules'                   => (new EntryRulesParser())->parse($this->stream),
57
            'dates'                   => (new EntryDatesParser())->parse($this->stream),
58
            'stream'                  => (new EntryStreamParser())->parse($this->stream),
59
            'trashable'               => (new EntryTrashableParser())->parse($this->stream),
60
            'relations'               => (new EntryRelationsParser())->parse($this->stream),
61
            'namespace'               => (new EntryNamespaceParser())->parse($this->stream),
62
            'field_slugs'             => (new EntryFieldSlugsParser())->parse($this->stream),
63
            'searchable'              => (new EntrySearchableParser())->parse($this->stream),
64
            'relationships'           => (new EntryRelationshipsParser())->parse($this->stream),
65
            'translation_model'       => (new EntryTranslationModelParser())->parse($this->stream),
66
            'translated_attributes'   => (new EntryTranslatedAttributesParser())->parse($this->stream),
67
            'translation_foreign_key' => (new EntryTranslationForeignKeyParser())->parse($this->stream),
68
        ];
69
70
        $template = file_get_contents(__DIR__ . '/../../../resources/stubs/models/entry.stub');
71
72
        $path = $application->getStoragePath('models/' . studly_case($this->stream->getNamespace()));
73
74
        $files->makeDirectory($path, 0777, true, true);
75
76
        $file = $path . '/' . studly_case($this->stream->getNamespace()) . studly_case($this->stream->getSlug()) . 'EntryModel.php';
77
78
        $files->makeDirectory(dirname($file), 0777, true, true);
79
        $files->delete($file);
80
81
        $files->put($file, $parser->parse($template, $data));
82
    }
83
}
84