Completed
Push — master ( 4317fb...cd535a )
by Vladimir
02:31
created

ConversationCreator::createIntent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 15
rs 9.4285
cc 1
eloc 8
nc 1
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace FondBot\Conversation;
6
7
use FondBot\Helpers\Str;
8
use FondBot\Contracts\Filesystem\Filesystem;
9
10
class ConversationCreator
11
{
12
    private $filesystem;
13
14
    public function __construct(Filesystem $filesystem)
15
    {
16
        $this->filesystem = $filesystem;
17
    }
18
19
    /**
20
     * Create new intent.
21
     *
22
     * @param string $directory
23
     * @param string $namespace
24
     * @param string $name
25
     */
26
    public function createIntent(string $directory, string $namespace, string $name): void
27
    {
28
        $contents = $this->filesystem->read(__DIR__.'/../../resources/stubs/Intent.stub');
29
30
        $className = $this->className($name, 'Intent');
31
32
        // Replace stub placeholders
33
        $this->replacePlaceholder($contents, 'namespace', $this->namespace($namespace));
34
        $this->replacePlaceholder($contents, 'className', $className);
35
        $this->replacePlaceholder($contents, 'name', $this->formatName($name));
36
37
        $path = $directory.'/'.$this->filename($className);
38
39
        $this->filesystem->write($path, $contents);
40
    }
41
42
    /**
43
     * Create new interaction.
44
     *
45
     * @param string $directory
46
     * @param string $namespace
47
     * @param string $name
48
     */
49
    public function createInteraction(string $directory, string $namespace, string $name): void
50
    {
51
        $contents = $this->filesystem->read(__DIR__.'/../../resources/stubs/Interaction.stub');
52
53
        $className = $this->className($name, 'Interaction');
54
55
        // Replace stub placeholders
56
        $this->replacePlaceholder($contents, 'namespace', $this->namespace($namespace, 'Interactions'));
57
        $this->replacePlaceholder($contents, 'className', $className);
58
59
        $path = $directory.'/Interactions/'.$this->filename($className);
60
61
        $this->filesystem->write($path, $contents);
62
    }
63
64
    /**
65
     * Replace placeholder.
66
     *
67
     * @param string $input
68
     * @param string $key
69
     * @param string $value
70
     */
71
    private function replacePlaceholder(string &$input, string $key, string $value): void
72
    {
73
        $key = mb_strtoupper($key);
74
        $input = str_replace('___'.$key.'___', $value, $input);
75
    }
76
77
    /**
78
     * Get filename.
79
     *
80
     * @param string $name
81
     *
82
     * @return string
83
     */
84
    private function filename(string $name): string
85
    {
86
        return $name.'.php';
87
    }
88
89
    /**
90
     * Get formatted name.
91
     *
92
     * @param string $name
93
     *
94
     * @return string
95
     */
96
    private function formatName(string $name): string
97
    {
98
        return mb_strtolower(trim($name));
99
    }
100
101
    /**
102
     * Get formatted namespace.
103
     *
104
     * @param string      $value
105
     *
106
     * @param string|null $postfix
107
     *
108
     * @return string
109
     */
110
    private function namespace(string $value, string $postfix = null): string
2 ignored issues
show
Coding Style introduced by
Possible parse error: non-abstract method defined as abstract
Loading history...
Coding Style introduced by
It is generally advisable to only define one property per statement.

Only declaring a single property per statement allows you to later on add doc comments more easily.

It is also recommended by PSR2, so it is a common style that many people expect.

Loading history...
111
    {
112
        if (Str::endsWith($value, '\\')) {
113
            $value = mb_substr($value, 0, -1);
114
        }
115
116
        if ($postfix !== null) {
0 ignored issues
show
Coding Style introduced by
It is generally advisable to only define one property per statement.

Only declaring a single property per statement allows you to later on add doc comments more easily.

It is also recommended by PSR2, so it is a common style that many people expect.

Loading history...
Coding Style introduced by
The visibility should be declared for property $postfix.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
117
            $value .= '\\'.$postfix;
118
        }
119
120
        return $value;
1 ignored issue
show
Coding Style introduced by
The visibility should be declared for property $value.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.

Loading history...
121
    }
122
123
    /**
124
     * Get name of class.
125
     *
126
     * @param string $name
127
     * @param string $postfix
128
     *
129
     * @return string
130
     */
131
    private function className(string $name, string $postfix): string
132
    {
133
        $name = trim($name);
134
        if (!Str::endsWith($name, $postfix)) {
135
            $name .= $postfix;
136
        }
137
138
        return $name;
139
    }
140
}
141