Completed
Push — master ( 7b302a...d24209 )
by Tristan
04:27
created

Manager::writeOut()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
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 File $file_dispatch A parrot file dispatch wrapper.
27
     */
28
    public function __construct(File $file_dispatch)
29
    {
30
        $this->stubs_dir = __DIR__.'/';
31
        $this->file_dispatch = $file_dispatch;
0 ignored issues
show
Documentation Bug introduced by
It seems like $file_dispatch of type object<Enzyme\Parrot\File> is incompatible with the declared type object<Enzyme\Axiom\Cons...ubs\Enzyme\Parrot\File> of property $file_dispatch.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
32
    }
33
34
    /**
35
     * Get the contents for the provided stub. Will throw an
36
     * \InvalidArgumentException if the stub by the given name does not exist.
37
     *
38
     * @param string $stub
39
     *
40
     * @return string
41
     */
42
    public function get($stub)
43
    {
44
        $file = $this->stubs_dir.$stub.'.stub';
45
46
        if (false === $this->file_dispatch->exists($file)) {
47
            throw new InvalidArgumentException(
48
                "The stub [$stub] does not exist"
49
            );
50
        }
51
52
        return $this->file_dispatch->getContents($file);
53
    }
54
55
    /**
56
     * Hydrate the given contents, replacing the given keys with the values
57
     * provided in the associative array.
58
     *
59
     * @param string $contents
60
     * @param array  $data
61
     *
62
     * @return string
63
     */
64
    public function hydrate($contents, array $data)
65
    {
66
        foreach ($data as $key => $value) {
67
            $contents = str_replace('%'.$key.'%', $value, $contents);
68
        }
69
70
        return $contents;
71
    }
72
73
    /**
74
     * Write the given contents out to the specified file.
75
     *
76
     * @param string $contents
77
     * @param string $path
78
     */
79
    public function writeOut($contents, $path)
80
    {
81
        $this->file_dispatch->putContents($path, $contents);
82
    }
83
}
84