prepareConnectionFormatSubForms()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 10
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 SprykerEco\Zed\PunchoutCatalogs\Communication\Form\DataProvider;
9
10
use Generated\Shared\Transfer\CompanyBusinessUnitCriteriaFilterTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...tCriteriaFilterTransfer 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 Generated\Shared\Transfer\CompanyBusinessUnitTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...anyBusinessUnitTransfer 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...
12
use Generated\Shared\Transfer\PunchoutCatalogConnectionCartTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...gConnectionCartTransfer 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...
13
use Generated\Shared\Transfer\PunchoutCatalogConnectionSetupTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...ConnectionSetupTransfer 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...
14
use Generated\Shared\Transfer\PunchoutCatalogConnectionTransfer;
0 ignored issues
show
Bug introduced by
The type Generated\Shared\Transfe...talogConnectionTransfer 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...
15
use SprykerEco\Zed\PunchoutCatalogs\Communication\Form\PunchoutCatalogConnectionForm;
16
use SprykerEco\Zed\PunchoutCatalogs\Dependency\Facade\PunchoutCatalogsToCompanyBusinessUnitFacadeInterface;
17
18
class PunchoutCatalogConnectionFormDataProvider
19
{
20
    /**
21
     * @var \SprykerEco\Zed\PunchoutCatalogs\Dependency\Facade\PunchoutCatalogsToCompanyBusinessUnitFacadeInterface
22
     */
23
    protected $companyBusinessUnitFacade;
24
25
    /**
26
     * @var \SprykerEco\Zed\PunchoutCatalogs\Dependency\Plugin\PunchoutCatalogConnectionFormatPluginInterface[]
27
     */
28
    protected $punchoutCatalogConnectionFormatPlugins;
29
30
    /**
31
     * @var \SprykerEco\Zed\PunchoutCatalogs\Dependency\Plugin\PunchoutCatalogConnectionTypePluginInterface[]
32
     */
33
    protected $punchoutCatalogConnectionTypePlugins;
34
35
    /**
36
     * @param \SprykerEco\Zed\PunchoutCatalogs\Dependency\Facade\PunchoutCatalogsToCompanyBusinessUnitFacadeInterface $companyBusinessUnitFacade
37
     * @param \SprykerEco\Zed\PunchoutCatalogs\Dependency\Plugin\PunchoutCatalogConnectionFormatPluginInterface[] $punchoutCatalogConnectionFormatPlugins
38
     * @param \SprykerEco\Zed\PunchoutCatalogs\Dependency\Plugin\PunchoutCatalogConnectionTypePluginInterface[] $punchoutCatalogConnectionTypePlugins
39
     */
40
    public function __construct(
41
        PunchoutCatalogsToCompanyBusinessUnitFacadeInterface $companyBusinessUnitFacade,
42
        array $punchoutCatalogConnectionFormatPlugins,
43
        array $punchoutCatalogConnectionTypePlugins
44
    ) {
45
        $this->companyBusinessUnitFacade = $companyBusinessUnitFacade;
46
        $this->punchoutCatalogConnectionFormatPlugins = $punchoutCatalogConnectionFormatPlugins;
47
        $this->punchoutCatalogConnectionTypePlugins = $punchoutCatalogConnectionTypePlugins;
48
    }
49
50
    /**
51
     * @return array
52
     */
53
    public function getOptions(): array
54
    {
55
        return [
56
            PunchoutCatalogConnectionForm::OPTION_BUSINESS_UNIT_CHOICES => $this->prepareCompanyBusinessUnitChoices(),
57
            PunchoutCatalogConnectionForm::OPTION_CONNECTION_FORMAT_SUB_FORM_TYPES => $this->prepareConnectionFormatSubForms(),
58
            PunchoutCatalogConnectionForm::OPTION_CONNECTION_TYPE_SUB_FORM_TYPES => $this->prepareConnectionTypeSubForms(),
59
        ];
60
    }
61
62
    /**
63
     * @param \Generated\Shared\Transfer\PunchoutCatalogConnectionTransfer|null $punchoutCatalogConnectionTransfer
64
     *
65
     * @return \Generated\Shared\Transfer\PunchoutCatalogConnectionTransfer
66
     */
67
    public function getData(?PunchoutCatalogConnectionTransfer $punchoutCatalogConnectionTransfer = null): PunchoutCatalogConnectionTransfer
68
    {
69
        if ($punchoutCatalogConnectionTransfer) {
70
            return $punchoutCatalogConnectionTransfer;
71
        }
72
73
        return (new PunchoutCatalogConnectionTransfer())
74
            ->setCart(new PunchoutCatalogConnectionCartTransfer())
75
            ->setSetup(new PunchoutCatalogConnectionSetupTransfer())
76
            ->setIsActive(true);
77
    }
78
79
    /**
80
     * @return int[] [label => idBusinessUnit]
81
     */
82
    protected function prepareCompanyBusinessUnitChoices(): array
83
    {
84
        $companyBusinessUnitCollection = $this->companyBusinessUnitFacade->getCompanyBusinessUnitCollection(
85
            (new CompanyBusinessUnitCriteriaFilterTransfer())
86
        );
87
88
        $companyBusinessUnitChoices = [];
89
90
        foreach ($companyBusinessUnitCollection->getCompanyBusinessUnits() as $companyBusinessUnitTransfer) {
91
            if (!$companyBusinessUnitTransfer->getCompany()->getIsActive()) {
92
                continue;
93
            }
94
95
            $companyBusinessUnitChoices[$this->buildCompanyBusinessUnitLabel($companyBusinessUnitTransfer)]
96
                = $companyBusinessUnitTransfer->getIdCompanyBusinessUnit();
97
        }
98
99
        return $companyBusinessUnitChoices;
100
    }
101
102
    /**
103
     * @return string[] [connectionFormat => FormTypePath]
104
     */
105
    protected function prepareConnectionFormatSubForms(): array
106
    {
107
        $connectionFormatSubForms = [];
108
109
        foreach ($this->punchoutCatalogConnectionFormatPlugins as $punchoutCatalogConnectionFormatPlugin) {
110
            $connectionFormatSubForms[$punchoutCatalogConnectionFormatPlugin->getConnectionFormat()]
111
                = $punchoutCatalogConnectionFormatPlugin->getType();
112
        }
113
114
        return $connectionFormatSubForms;
115
    }
116
117
    /**
118
     * @return string[] [connectionType => FormTypePath]
119
     */
120
    protected function prepareConnectionTypeSubForms(): array
121
    {
122
        $connectionTypeSubForms = [];
123
124
        foreach ($this->punchoutCatalogConnectionTypePlugins as $punchoutCatalogConnectionTypePlugin) {
125
            $connectionTypeSubForms[$punchoutCatalogConnectionTypePlugin->getConnectionType()]
126
                = $punchoutCatalogConnectionTypePlugin->getType();
127
        }
128
129
        return $connectionTypeSubForms;
130
    }
131
132
    /**
133
     * @param \Generated\Shared\Transfer\CompanyBusinessUnitTransfer $companyBusinessUnitTransfer
134
     *
135
     * @return string
136
     */
137
    protected function buildCompanyBusinessUnitLabel(CompanyBusinessUnitTransfer $companyBusinessUnitTransfer): string
138
    {
139
        return sprintf(
140
            '%s-%s',
141
            $companyBusinessUnitTransfer->getCompany()
142
                ->getName(),
143
            $companyBusinessUnitTransfer->getName()
144
        );
145
    }
146
}
147