GraphQLSubscriptionGenerator::generate()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
dl 0
loc 13
rs 10
c 1
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
namespace PWWEB\Artomator\Generators\GraphQL;
4
5
use Illuminate\Support\Str;
6
use InfyOm\Generator\Generators\BaseGenerator;
7
use PWWEB\Artomator\Common\CommandData;
8
9
class GraphQLSubscriptionGenerator extends BaseGenerator
10
{
11
    /**
12
     * Command Data.
13
     *
14
     * @var CommandData
15
     */
16
    private $commandData;
17
18
    /**
19
     * Filename.
20
     *
21
     * @var string
22
     */
23
    private $fileName;
24
25
    /**
26
     * File Contents.
27
     *
28
     * @var string
29
     */
30
    private $fileContents;
31
32
    /**
33
     * Template Data.
34
     *
35
     * @var string
36
     */
37
    private $templateData;
38
39
    /**
40
     * Constructor.
41
     *
42
     * @param CommandData $commandData Command Data.
43
     */
44
    public function __construct(CommandData $commandData)
45
    {
46
        $this->commandData = $commandData;
47
        $this->fileName = $commandData->config->pathGraphQL;
48
        $this->fileContents = file_get_contents($this->fileName);
49
        $this->templateData = get_artomator_template('graphql.subscription');
50
        $this->templateData = fill_template($this->commandData->dynamicVars, $this->templateData);
51
    }
52
53
    /**
54
     * Generate.
55
     *
56
     * @return void
57
     */
58
    public function generate()
59
    {
60
        if (true === Str::contains($this->fileContents, $this->templateData)) {
61
            $this->commandData->commandObj->info('GraphQL Subscription '.$this->commandData->config->mHumanPlural.' already exists; Skipping');
62
63
            return;
64
        }
65
66
        $this->fileContents = preg_replace('/(type Subscription {)(.+?[^}])(})/is', '$1$2'.$this->templateData.'$3', $this->fileContents);
67
68
        file_put_contents($this->fileName, $this->fileContents);
69
70
        $this->commandData->commandComment("\nGraphQL Subscription created");
71
    }
72
73
    /**
74
     * Rollback.
75
     *
76
     * @return void
77
     */
78
    public function rollback()
79
    {
80
        if (true === Str::contains($this->fileContents, $this->templateData)) {
81
            file_put_contents($this->fileName, str_replace($this->templateData, '', $this->fileContents));
82
            $this->commandData->commandComment('GraphQL Subscription deleted');
83
        }
84
    }
85
}
86