Completed
Pull Request — master (#1)
by Grebenikov
14:36
created

FileWriter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 3
1
<?php
2
3
/**
4
 * MIT License
5
 * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
6
 */
7
namespace SprykerEco\Zed\Minubo\Business\Writer;
8
9
use Generated\Shared\Transfer\FileSystemContentTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...leSystemContentTransfer was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use SprykerEco\Zed\Minubo\Dependency\Service\MinuboToFileSystemServiceInterface;
11
use SprykerEco\Zed\Minubo\Dependency\Service\MinuboToUtilEncodingServiceInterface;
12
use SprykerEco\Zed\Minubo\MinuboConfig;
13
14
class FileWriter implements WriterInterface
15
{
16
    /**
17
     * @var \SprykerEco\Zed\Minubo\Dependency\Service\MinuboToUtilEncodingServiceInterface
18
     */
19
    protected $utilEncodingService;
20
21
    /**
22
     * @var \SprykerEco\Zed\Minubo\Dependency\Service\MinuboToFileSystemServiceInterface
23
     */
24
    protected $fileSystemService;
25
26
    /**
27
     * @var \SprykerEco\Zed\Minubo\MinuboConfig
28
     */
29
    protected $config;
30
31
    /**
32
     * @param \SprykerEco\Zed\Minubo\Dependency\Service\MinuboToUtilEncodingServiceInterface $utilEncodingService
33
     * @param \SprykerEco\Zed\Minubo\Dependency\Service\MinuboToFileSystemServiceInterface $fileSystemService
34
     * @param \SprykerEco\Zed\Minubo\MinuboConfig $config
35
     */
36
    public function __construct(
37
        MinuboToUtilEncodingServiceInterface $utilEncodingService,
38
        MinuboToFileSystemServiceInterface $fileSystemService,
39
        MinuboConfig $config
40
    ) {
41
        $this->utilEncodingService = $utilEncodingService;
42
        $this->fileSystemService = $fileSystemService;
43
        $this->config = $config;
44
    }
45
46
    /**
47
     * @param array $data
48
     * @param string $filePrefix
49
     *
50
     * @return void
51
     */
52
    public function writeData(array $data, string $filePrefix): void
53
    {
54
        $content = '';
55
        foreach ($data as $item) {
56
            $content .= $this->utilEncodingService->encodeJson($item) . PHP_EOL;
57
        }
58
59
        $this->writeContentToFile($content, $filePrefix);
60
    }
61
62
    /**
63
     * @param string $content
64
     * @param string $filePrefix
65
     *
66
     * @return void
67
     */
68
    protected function writeContentToFile(string $content, string $filePrefix): void
69
    {
70
        $fileSystemContentTransfer = new FileSystemContentTransfer();
71
        $fileSystemContentTransfer->setFileSystemName($this->config->getFileSystemName());
72
        $fileSystemContentTransfer->setContent($content);
73
        $fileSystemContentTransfer->setPath($this->getFileName($filePrefix));
74
75
        $this->fileSystemService->put($fileSystemContentTransfer);
76
    }
77
78
    /**
79
     * @param string $filePrefix
80
     *
81
     * @return string
82
     */
83
    protected function getFileName(string $filePrefix): string
84
    {
85
        return $this->config->getBucketDirectory() . $filePrefix . '_' . time() . '.json';
86
    }
87
}
88