Manager::hydrate()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
3
namespace Enzyme\Axiom\Console\Stubs;
4
5
use Enzyme\Parrot\File;
6
7
class Manager
8
{
9
    /**
10
     * The location of the stubs directory.
11
     *
12
     * @var string
13
     */
14
    protected $stubs_dir;
15
16
    /**
17
     * An instance of a parrot file wrapper.
18
     *
19
     * @var \Enzyme\Parrot\File
20
     */
21
    protected $file_dispatch;
22
23
    /**
24
     * Create a new stub manager.
25
     *
26
     * @param \Enzyme\Parrot\File $file_dispatch
27
     */
28
    public function __construct(File $file_dispatch)
29
    {
30
        $this->stubs_dir = __DIR__ . '/';
31
        $this->file_dispatch = $file_dispatch;
32
    }
33
34
    /**
35
     * Get the contents for the provided stub.
36
     *
37
     * @param string $stub
38
     *
39
     * @return string
40
     *
41
     * @throws InvalidArgumentException If the stub by the given name does not
42
     * exist in the `console/Stubs` folder using the format `{name}.stub`.
43
     */
44
    public function get($stub)
45
    {
46
        $file = $this->stubs_dir . $stub . '.stub';
47
48
        if (false === $this->file_dispatch->exists($file)) {
49
            throw new InvalidArgumentException(
50
                "The stub [$stub] does not exist"
51
            );
52
        }
53
54
        return $this->file_dispatch->getContents($file);
55
    }
56
57
    /**
58
     * Hydrate the given contents, replacing the given keys with the values
59
     * provided in the associative array.
60
     *
61
     * @param string $contents
62
     * @param array  $data
63
     *
64
     * @return string
65
     */
66
    public function hydrate($contents, array $data)
67
    {
68
        foreach ($data as $key => $value) {
69
            $contents = str_replace('%' . $key . '%', $value, $contents);
70
        }
71
72
        return $contents;
73
    }
74
75
    /**
76
     * Write the given contents out to the specified file.
77
     *
78
     * @param string $contents
79
     * @param string $path
80
     */
81
    public function writeOut($contents, $path)
82
    {
83
        $this->file_dispatch->putContents($path, $contents);
84
    }
85
}
86