FileWriter::writeContentToFile()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 5
c 1
b 0
f 1
dl 0
loc 8
rs 10
cc 1
nc 1
nop 2
1
<?php
2
3
/**
4
 * MIT License
5
 * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
6
 */
7
8
namespace SprykerEco\Zed\Minubo\Business\Writer;
9
10
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...
11
use SprykerEco\Zed\Minubo\Dependency\Service\MinuboToFileSystemServiceInterface;
12
use SprykerEco\Zed\Minubo\Dependency\Service\MinuboToUtilEncodingServiceInterface;
13
use SprykerEco\Zed\Minubo\MinuboConfig;
14
15
class FileWriter implements WriterInterface
16
{
17
    /**
18
     * @var \SprykerEco\Zed\Minubo\Dependency\Service\MinuboToUtilEncodingServiceInterface
19
     */
20
    protected $utilEncodingService;
21
22
    /**
23
     * @var \SprykerEco\Zed\Minubo\Dependency\Service\MinuboToFileSystemServiceInterface
24
     */
25
    protected $fileSystemService;
26
27
    /**
28
     * @var \SprykerEco\Zed\Minubo\MinuboConfig
29
     */
30
    protected $config;
31
32
    /**
33
     * @param \SprykerEco\Zed\Minubo\Dependency\Service\MinuboToUtilEncodingServiceInterface $utilEncodingService
34
     * @param \SprykerEco\Zed\Minubo\Dependency\Service\MinuboToFileSystemServiceInterface $fileSystemService
35
     * @param \SprykerEco\Zed\Minubo\MinuboConfig $config
36
     */
37
    public function __construct(
38
        MinuboToUtilEncodingServiceInterface $utilEncodingService,
39
        MinuboToFileSystemServiceInterface $fileSystemService,
40
        MinuboConfig $config
41
    ) {
42
        $this->utilEncodingService = $utilEncodingService;
43
        $this->fileSystemService = $fileSystemService;
44
        $this->config = $config;
45
    }
46
47
    /**
48
     * @param array $data
49
     * @param string $filePrefix
50
     *
51
     * @return void
52
     */
53
    public function writeData(array $data, string $filePrefix): void
54
    {
55
        $content = '';
56
        foreach ($data as $item) {
57
            $content .= $this->utilEncodingService->encodeJson($item) . PHP_EOL;
58
        }
59
60
        $this->writeContentToFile($content, $filePrefix);
61
    }
62
63
    /**
64
     * @param string $content
65
     * @param string $filePrefix
66
     *
67
     * @return void
68
     */
69
    protected function writeContentToFile(string $content, string $filePrefix): void
70
    {
71
        $fileSystemContentTransfer = new FileSystemContentTransfer();
72
        $fileSystemContentTransfer->setFileSystemName($this->config->getFileSystemName());
73
        $fileSystemContentTransfer->setContent($content);
74
        $fileSystemContentTransfer->setPath($this->getFileName($filePrefix));
75
76
        $this->fileSystemService->write($fileSystemContentTransfer);
77
    }
78
79
    /**
80
     * @param string $filePrefix
81
     *
82
     * @return string
83
     */
84
    protected function getFileName(string $filePrefix): string
85
    {
86
        return $this->config->getBucketDirectory() . $filePrefix . '_' . time() . '.json';
87
    }
88
}
89