Test Setup Failed
Push — develop ( 17b215...d74e9d )
by Freddie
13:13
created

ProcessFormatUseCase::execute()   B

Complexity

Conditions 9
Paths 9

Size

Total Lines 79
Code Lines 44

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 37
CRAP Score 9

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 9
eloc 44
c 2
b 0
f 0
nc 9
nop 1
dl 0
loc 79
ccs 37
cts 37
cp 1
crap 9
rs 7.6604

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php declare(strict_types=1);
2
/*
3
 * This file is part of FlexPHP.
4
 *
5
 * (c) Freddie Gar <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace FlexPHP\Generator\Domain\UseCases;
11
12
use Box\Spout\Reader\Common\Creator\ReaderEntityFactory;
13
use FlexPHP\Generator\Domain\Exceptions\FormatNotSupportedException;
14
use FlexPHP\Generator\Domain\Exceptions\FormatPathNotValidException;
15
use FlexPHP\Generator\Domain\Messages\Requests\ProcessFormatRequest;
16
use FlexPHP\Generator\Domain\Messages\Responses\ProcessFormatResponse;
17
use FlexPHP\Generator\Domain\Validations\FieldSyntaxValidation;
18
use FlexPHP\Generator\Domain\Validations\HeaderSyntaxValidation;
19
use FlexPHP\Generator\Domain\Writers\YamlWriter;
20
use FlexPHP\Schema\Constants\Keyword;
21
use FlexPHP\UseCases\UseCase;
22
use Jawira\CaseConverter\Convert;
23
24
final class ProcessFormatUseCase extends UseCase
25
{
26
    /**
27
     * Process format based in extension
28 12
     * - Syntax Sheet
29
     *
30 12
     * @param ProcessFormatRequest $request
31
     *
32 11
     * @throws FormatPathNotValidException
33 11
     * @throws FormatNotSupportedException
34 11
     *
35
     * @return ProcessFormatResponse
36 11
     */
37 7
    public function execute($request)
38
    {
39
        $this->throwExceptionIfRequestNotValid(__METHOD__, ProcessFormatRequest::class, $request);
40
41 4
        $sheetNames = [];
42
        $path = $request->path;
43 2
        $extension = $request->extension;
44
45 2
        if (!\is_file($path)) {
46 2
            throw new FormatPathNotValidException();
47
        }
48 2
49 2
        switch ($extension) {
50 2
            case 'xlsx': // MS Excel >= 2007
51
                $reader = ReaderEntityFactory::createXLSXReader();
52
53 2
                break;
54
            // case 'ods': // Open Format
55 2
            //     $reader = ReaderEntityFactory::createODSReader();
56 2
57
            //     break;
58 2
            default:
59 2
                throw new FormatNotSupportedException();
60 2
        }
61
62 2
        $reader->open($path);
63 2
64 2
        foreach ($reader->getSheetIterator() as $sheet) {
65
            if (!$sheet->isVisible()) {
66
                continue;
67 2
            }
68 2
69
            $sheetName = (new Convert($sheet->getName()))->toPascal();
70 2
71
            $headers = [];
72
            $fields = [];
73 2
74
            foreach ($sheet->getRowIterator() as $rowNumber => $row) {
75 2
                $rowNumber -= 1;
76 2
                $cols = $row->getCells();
77
78
                if ($rowNumber === 0) {
79 2
                    foreach ($cols as $colNumber => $col) {
80 2
                        $headers[$colNumber] = $col->getValue();
81
                    }
82 2
83 2
                    $headerValidation = new HeaderSyntaxValidation($headers);
84 2
                    $headerValidation->validate();
85
86
                    continue;
87 2
                }
88
89 2
                $field = [];
90 2
91
                foreach ($cols as $colNumber => $col) {
92 2
                    $field[$headers[$colNumber]] = $col->getValue();
93
                }
94 2
95
                $fieldValidation = new FieldSyntaxValidation($field);
96 2
                $fieldValidation->validate();
97
98 2
                $colHeaderName = $headers[\array_search(Keyword::NAME, $headers)];
99
                $fieldName = (new Convert($field[$colHeaderName]))->toCamel();
100 2
                $fields[$fieldName] = $field;
101
            }
102
103 2
            $writer = new YamlWriter([
104
                $sheetName => [
105
                    'Entity' => $sheetName,
106
                    'Attributes' => $fields,
107
                ],
108
            ], \strtolower($sheetName));
109
110
            $writer->save();
111
112
            $sheetNames[$sheetName] = \count($fields);
113
        }
114
115
        return new ProcessFormatResponse($sheetNames);
116
    }
117
}
118