Completed
Push — master ( d81c19...f57266 )
by Kamil
20s
created

src/Sylius/Behat/Service/ResponseLoader.php (1 issue)

Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Sylius\Behat\Service;
15
16
class ResponseLoader implements ResponseLoaderInterface
17
{
18
    /**
19
     * {@inheritdoc}
20
     */
21
    public function getMockedResponse($source)
22
    {
23
        $source = $this->getMockedResponsesFolder() . '/' . $source;
24
25
        return (array) json_decode($this->getFileContents($source));
26
    }
27
28
    /**
29
     * {@inheritdoc}
30
     */
31
    public function getExpectedResponse($source)
32
    {
33
        $source = $this->getExpectedResponsesFolder() . '/' . $source;
34
35
        return (array) json_decode($this->getFileContents($source));
36
    }
37
38
    /**
39
     * @return string
40
     */
41
    private function getResponsesFolder()
42
    {
43
        return $this->getCalledClassFolder() . '/Responses';
44
    }
45
46
    /**
47
     * @return string
48
     */
49
    private function getMockedResponsesFolder()
50
    {
51
        return $this->getResponsesFolder() . '/Mocked';
52
    }
53
54
    /**
55
     * @return string
56
     */
57
    private function getExpectedResponsesFolder()
58
    {
59
        return $this->getResponsesFolder() . '/Expected';
60
    }
61
62
    /**
63
     * @return string
64
     */
65
    private function getCalledClassFolder()
66
    {
67
        $calledClass = get_called_class();
68
        $calledClassFolder = dirname((new \ReflectionClass($calledClass))->getFileName());
69
70
        return $calledClassFolder;
71
    }
72
73
    /**
74
     * @param string $source
75
     *
76
     * @throws \RuntimeException
77
     */
78
    private function assertSourceExists($source)
79
    {
80
        if (!file_exists($source)) {
81
            throw new \RuntimeException(sprintf('File %s does not exist', $source));
82
        }
83
    }
84
85
    /**
86
     * @param string $source
87
     * @param mixed $content
88
     *
89
     * @throws \RuntimeException
90
     */
91
    private function assertContentIsNotEmpty($source, $content)
92
    {
93
        if ('' === $content) {
94
            throw new \RuntimeException(sprintf('Something went wrong, file %s is empty', $source));
95
        }
96
    }
97
98
    /**
99
     * @param string $source
100
     * @param mixed $content
101
     *
102
     * @throws \RuntimeException
103
     */
104
    private function assertContentIsProperLoaded($source, $content)
105
    {
106
        if (false === $content) {
107
            throw new \RuntimeException(sprintf('Something went wrong, cannot open %s', $source));
108
        }
109
    }
110
111
    /**
112
     * @param string $source
113
     *
114
     * @throws \RuntimeException
115
     */
116
    private function assertSourceIsNotFolder($source)
117
    {
118
        if (true === is_dir($source)) {
119
            throw new \RuntimeException(sprintf('Given source %s is a folder!', $source));
120
        }
121
    }
122
123
    /**
124
     * @param string $source
125
     *
126
     * @return string
127
     *
128
     * @throws \RuntimeException
129
     */
130
    private function getFileContents($source)
0 ignored issues
show
This method is not used, and could be removed.
Loading history...
131
    {
132
        $this->assertSourceExists($source);
133
        $this->assertSourceIsNotFolder($source);
134
        $content = file_get_contents($source, true);
135
136
        $this->assertContentIsProperLoaded($source, $content);
137
        $this->assertContentIsNotEmpty($source, $content);
138
139
        return $content;
140
    }
141
}
142