Failed Conditions
Push — master ( 36cd34...e25ade )
by
unknown
47:16 queued 16:37
created

FileWriter::translateMessage()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 4
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 9
rs 10
1
<?php
2
3
/**
4
 * Copyright © 2016-present Spryker Systems GmbH. All rights reserved.
5
 * Use of this software requires acceptance of the Evaluation License Agreement. See LICENSE file.
6
 */
7
8
namespace Spryker\Zed\DataImportMerchantPortalGui\Communication\Writer;
9
10
use Generated\Shared\Transfer\ErrorTransfer;
11
use Spryker\Zed\DataImportMerchantPortalGui\Dependency\Facade\DataImportMerchantPortalGuiToTranslatorFacadeInterface;
12
13
class FileWriter implements FileWriterInterface
14
{
15
    /**
16
     * @param \Spryker\Zed\DataImportMerchantPortalGui\Dependency\Facade\DataImportMerchantPortalGuiToTranslatorFacadeInterface $translatorFacade
17
     */
18
    public function __construct(
19
        protected DataImportMerchantPortalGuiToTranslatorFacadeInterface $translatorFacade
20
    ) {
21
    }
22
23
    /**
24
     * @param mixed|resource $fileStream
25
     *
26
     * @return callable
27
     */
28
    public function write($fileStream): callable
29
    {
30
        return static function () use ($fileStream): void {
31
            /** @var resource $outputStream */
32
            $outputStream = fopen('php://output', 'w');
33
34
            stream_copy_to_stream($fileStream, $outputStream);
35
        };
36
    }
37
38
    /**
39
     * @param array<array<string, string>> $errors
40
     *
41
     * @return callable
42
     */
43
    public function writeErrors(array $errors): callable
44
    {
45
        return function () use ($errors): void {
46
            /** @var resource $outputStream */
47
            $outputStream = fopen('php://output', 'w');
48
49
            fputcsv($outputStream, [
50
                $this->translatorFacade->trans('row_number'),
51
                $this->translatorFacade->trans('identifier'),
52
                $this->translatorFacade->trans('message'),
53
            ]);
54
55
            try {
56
                foreach ($errors as $error) {
57
                    fputcsv(
58
                        $outputStream,
59
                        [
60
                            $error['row_number'] ?? '',
61
                            $error['identifier'] ?? '',
62
                            $this->translateMessage($error),
63
                        ],
64
                    );
65
                }
66
            } finally {
67
                fclose($outputStream);
68
            }
69
        };
70
    }
71
72
    /**
73
     * @param array<string, mixed> $error
74
     *
75
     * @return string
76
     */
77
    protected function translateMessage(array $error): string
78
    {
79
        if (!isset($error['error'])) {
80
            return $error['message'] ? $this->translatorFacade->trans($error['message']) : '';
81
        }
82
83
        $errorTransfer = (new ErrorTransfer())->fromArray($error['error'], true);
84
85
        return $this->translatorFacade->trans($errorTransfer->getMessageOrFail(), $errorTransfer->getParameters());
86
    }
87
}
88