PunchoutCatalogsConnectionsTable::getStatusLabel()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 3
c 1
b 0
f 1
nc 2
nop 1
dl 0
loc 7
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\Table;
9
10
use Orm\Zed\Company\Persistence\Map\SpyCompanyTableMap;
0 ignored issues
show
Bug introduced by
The type Orm\Zed\Company\Persistence\Map\SpyCompanyTableMap 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 Orm\Zed\CompanyBusinessUnit\Persistence\Map\SpyCompanyBusinessUnitTableMap;
0 ignored issues
show
Bug introduced by
The type Orm\Zed\CompanyBusinessU...anyBusinessUnitTableMap 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 Orm\Zed\PunchoutCatalog\Persistence\Map\PgwPunchoutCatalogConnectionTableMap;
0 ignored issues
show
Bug introduced by
The type Orm\Zed\PunchoutCatalog\...talogConnectionTableMap 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 Orm\Zed\PunchoutCatalog\Persistence\PgwPunchoutCatalogConnection;
0 ignored issues
show
Bug introduced by
The type Orm\Zed\PunchoutCatalog\...nchoutCatalogConnection 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 Orm\Zed\PunchoutCatalog\Persistence\PgwPunchoutCatalogConnectionQuery;
0 ignored issues
show
Bug introduced by
The type Orm\Zed\PunchoutCatalog\...tCatalogConnectionQuery 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 Propel\Runtime\Collection\ObjectCollection;
16
use Spryker\Service\UtilText\Model\Url\Url;
17
use Spryker\Zed\Gui\Communication\Table\AbstractTable;
18
use Spryker\Zed\Gui\Communication\Table\TableConfiguration;
19
use SprykerEco\Zed\PunchoutCatalogs\Dependency\Service\PunchoutCatalogsToUtilDateTimeServiceInterface;
20
21
class PunchoutCatalogsConnectionsTable extends AbstractTable
22
{
23
    protected const STATUS_ACTIVE = 'Active';
24
    protected const STATUS_INACTIVE = 'Inactive';
25
26
    protected const COL_ID_PUNCHOUT_CATALOG_CONNECTION = 'id_punchout_catalog_connection';
27
    protected const COL_NAME = 'name';
28
    protected const COL_COMPANY = 'company';
29
    protected const COL_BUSINESS_UNIT = 'business_unit';
30
    protected const COL_TYPE = 'type';
31
    protected const COL_FORMAT = 'format';
32
    protected const COL_CREATED_AT = 'created_at';
33
    protected const COL_STATUS = 'is_active';
34
    protected const COL_ACTIONS = 'actions';
35
36
    protected const URL_PARAM_ID_PUNCHOUT_CATALOG_CONNECTION = 'id-punchout-catalog-connection';
37
38
    /**
39
     * @see \SprykerEco\Zed\PunchoutCatalogs\Communication\Controller\IndexController::editAction()
40
     */
41
    protected const URL_EDIT_PUNCHOUT_CATALOG_CONNECTION = '/punchout-catalogs/index/edit';
42
43
    /**
44
     * @see \SprykerEco\Zed\PunchoutCatalogs\Communication\Controller\EntryPointsController::viewAction()
45
     */
46
    protected const URL_VIEW_PUNCHOUT_CATALOG_ENTRY_POINTS = '/punchout-catalogs/entry-points/view';
47
48
    /**
49
     * @see \SprykerEco\Zed\PunchoutCatalogs\Communication\Controller\ConnectionController::activateAction()
50
     */
51
    protected const ROUTE_PUNCHOUT_CATALOG_CONNECTION_ACTIVATE = '/punchout-catalogs/connection/activate';
52
53
    /**
54
     * @see \SprykerEco\Zed\PunchoutCatalogs\Communication\Controller\ConnectionController::deactivateAction()
55
     */
56
    protected const ROUTE_PUNCHOUT_CATALOG_CONNECTION_DEACTIVATE = '/punchout-catalogs/connection/deactivate';
57
58
    /**
59
     * @var \Orm\Zed\PunchoutCatalog\Persistence\PgwPunchoutCatalogConnectionQuery
60
     */
61
    protected $connectionPropelQuery;
62
63
    /**
64
     * @var \SprykerEco\Zed\PunchoutCatalogs\Dependency\Service\PunchoutCatalogsToUtilDateTimeServiceInterface
65
     */
66
    protected $utilDateTimeService;
67
68
    /**
69
     * @param \Orm\Zed\PunchoutCatalog\Persistence\PgwPunchoutCatalogConnectionQuery $punchoutCatalogConnectionPropelQuery
70
     * @param \SprykerEco\Zed\PunchoutCatalogs\Dependency\Service\PunchoutCatalogsToUtilDateTimeServiceInterface $utilDateTimeService
71
     */
72
    public function __construct(PgwPunchoutCatalogConnectionQuery $punchoutCatalogConnectionPropelQuery, PunchoutCatalogsToUtilDateTimeServiceInterface $utilDateTimeService)
73
    {
74
        $this->connectionPropelQuery = $punchoutCatalogConnectionPropelQuery;
75
        $this->utilDateTimeService = $utilDateTimeService;
76
    }
77
78
    /**
79
     * @param \Spryker\Zed\Gui\Communication\Table\TableConfiguration $config
80
     *
81
     * @return \Spryker\Zed\Gui\Communication\Table\TableConfiguration
82
     */
83
    protected function configure(TableConfiguration $config): TableConfiguration
84
    {
85
        $config->setHeader([
86
            static::COL_ID_PUNCHOUT_CATALOG_CONNECTION => '#',
87
            static::COL_NAME => 'Name',
88
            static::COL_STATUS => 'Status',
89
            static::COL_TYPE => 'Type',
90
            static::COL_COMPANY => 'Company',
91
            static::COL_BUSINESS_UNIT => 'Business Unit',
92
            static::COL_FORMAT => 'Format',
93
            static::COL_CREATED_AT => 'Created At',
94
            static::COL_ACTIONS => 'Actions',
95
        ]);
96
97
        $config->setSortable([
98
            static::COL_ID_PUNCHOUT_CATALOG_CONNECTION,
99
            static::COL_NAME,
100
            static::COL_TYPE,
101
            static::COL_COMPANY,
102
            static::COL_BUSINESS_UNIT,
103
            static::COL_STATUS,
104
            static::COL_FORMAT,
105
            static::COL_CREATED_AT,
106
        ]);
107
108
        $config->setSearchable([
109
            PgwPunchoutCatalogConnectionTableMap::COL_ID_PUNCHOUT_CATALOG_CONNECTION,
110
            PgwPunchoutCatalogConnectionTableMap::COL_NAME,
111
            SpyCompanyTableMap::COL_NAME,
112
            SpyCompanyBusinessUnitTableMap::COL_NAME,
113
        ]);
114
115
        $config->setRawColumns([
116
            static::COL_STATUS,
117
            static::COL_FORMAT,
118
            static::COL_ACTIONS,
119
            static::COL_TYPE,
120
        ]);
121
122
        return $config;
123
    }
124
125
    /**
126
     * @param \Spryker\Zed\Gui\Communication\Table\TableConfiguration $config
127
     *
128
     * @return array
129
     */
130
    protected function prepareData(TableConfiguration $config): array
131
    {
132
        /** @var \Propel\Runtime\Collection\ObjectCollection|\Orm\Zed\PunchoutCatalog\Persistence\PgwPunchoutCatalogConnection[] $punchoutCatalogConnectionCollection */
133
        $punchoutCatalogConnectionCollection = $this->runQuery(
134
            $this->prepareQuery($this->connectionPropelQuery),
135
            $config,
136
            true
137
        );
138
139
        if ($punchoutCatalogConnectionCollection->count() === 0) {
140
            return [];
141
        }
142
143
        return $this->mapPunchoutCatalogConnections($punchoutCatalogConnectionCollection);
144
    }
145
146
    /**
147
     * @module Company
148
     * @module CompanyBusinessUnit
149
     *
150
     * @param \Orm\Zed\PunchoutCatalog\Persistence\PgwPunchoutCatalogConnectionQuery $connectionPropelQuery
151
     *
152
     * @return \Orm\Zed\PunchoutCatalog\Persistence\PgwPunchoutCatalogConnectionQuery
153
     */
154
    protected function prepareQuery(PgwPunchoutCatalogConnectionQuery $connectionPropelQuery): PgwPunchoutCatalogConnectionQuery
155
    {
156
         $connectionPropelQuery
157
            ->joinWithCompanyBusinessUnit()
158
            ->useCompanyBusinessUnitQuery()
159
                 ->withColumn(SpyCompanyBusinessUnitTableMap::COL_NAME, static::COL_BUSINESS_UNIT)
160
                 ->joinCompany()
161
                 ->useCompanyQuery()
162
                        ->withColumn(SpyCompanyTableMap::COL_NAME, static::COL_COMPANY)
163
                ->endUse()
164
            ->endUse();
165
166
        return $connectionPropelQuery;
167
    }
168
169
    /**
170
     * @param \Propel\Runtime\Collection\ObjectCollection|\Orm\Zed\PunchoutCatalog\Persistence\PgwPunchoutCatalogConnection[] $punchoutCatalogConnectionCollection
171
     *
172
     * @return array
173
     */
174
    protected function mapPunchoutCatalogConnections(ObjectCollection $punchoutCatalogConnectionCollection): array
175
    {
176
        $punchoutCatalogConnectionRows = [];
177
178
        foreach ($punchoutCatalogConnectionCollection as $punchoutCatalogConnection) {
179
            $punchoutCatalogConnectionRows[] = $this->mapPunchoutCatalogConnection($punchoutCatalogConnection);
180
        }
181
182
        return $punchoutCatalogConnectionRows;
183
    }
184
185
    /**
186
     * @param \Orm\Zed\PunchoutCatalog\Persistence\PgwPunchoutCatalogConnection $punchoutCatalogConnection
187
     *
188
     * @return array
189
     */
190
    protected function mapPunchoutCatalogConnection(PgwPunchoutCatalogConnection $punchoutCatalogConnection): array
191
    {
192
        $punchoutCatalogConnectionRow = $punchoutCatalogConnection->toArray();
193
194
        $punchoutCatalogConnectionRow[static::COL_ACTIONS] = $this->buildLinks($punchoutCatalogConnection);
195
        $punchoutCatalogConnectionRow[static::COL_STATUS] = $this->getStatusLabel($punchoutCatalogConnection);
196
        $punchoutCatalogConnectionRow[static::COL_FORMAT] = $this->generateLabel(
197
            $punchoutCatalogConnection->getFormat(),
198
            'label-secondary'
199
        );
200
        $punchoutCatalogConnectionRow[static::COL_TYPE] = $this->generateLabel(
201
            $punchoutCatalogConnection->getType(),
202
            'label-secondary'
203
        );
204
        $punchoutCatalogConnectionRow[static::COL_CREATED_AT] = $this->utilDateTimeService->formatDateTime(
205
            $punchoutCatalogConnection->getCreatedAt()
206
        );
207
        $punchoutCatalogConnectionRow[static::COL_COMPANY] = $punchoutCatalogConnection->getCompanyBusinessUnit()
208
            ->getCompany()
209
            ->getName();
210
211
        return $punchoutCatalogConnectionRow;
212
    }
213
214
    /**
215
     * @param \Orm\Zed\PunchoutCatalog\Persistence\PgwPunchoutCatalogConnection $punchoutCatalogConnection
216
     *
217
     * @return string
218
     */
219
    protected function buildLinks(PgwPunchoutCatalogConnection $punchoutCatalogConnection): string
220
    {
221
        $buttons = [];
222
223
        $buttons[] = $this->generateEditButton(
224
            Url::generate(static::URL_EDIT_PUNCHOUT_CATALOG_CONNECTION, [
225
                static::URL_PARAM_ID_PUNCHOUT_CATALOG_CONNECTION => $punchoutCatalogConnection->getIdPunchoutCatalogConnection(),
226
            ]),
227
            'Edit'
228
        );
229
230
        $buttons[] = $this->generateViewButton(
231
            Url::generate(static::URL_VIEW_PUNCHOUT_CATALOG_ENTRY_POINTS, [
232
                static::URL_PARAM_ID_PUNCHOUT_CATALOG_CONNECTION => $punchoutCatalogConnection->getIdPunchoutCatalogConnection(),
233
            ]),
234
            'Entry Points'
235
        );
236
237
        $buttons[] = $this->generateConnectionStatusChangeButton($punchoutCatalogConnection);
238
239
        return implode(' ', $buttons);
240
    }
241
242
    /**
243
     * @param \Orm\Zed\PunchoutCatalog\Persistence\PgwPunchoutCatalogConnection $punchoutCatalogConnection
244
     *
245
     * @return string
246
     */
247
    protected function generateConnectionStatusChangeButton(PgwPunchoutCatalogConnection $punchoutCatalogConnection): string
248
    {
249
        if ($punchoutCatalogConnection->getIsActive()) {
250
            return $this->generateRemoveButton(
251
                Url::generate(static::ROUTE_PUNCHOUT_CATALOG_CONNECTION_DEACTIVATE, [
252
                    static::URL_PARAM_ID_PUNCHOUT_CATALOG_CONNECTION => $punchoutCatalogConnection->getIdPunchoutCatalogConnection(),
253
                ]),
254
                'Deactivate'
255
            );
256
        }
257
258
        return $this->generateViewButton(
259
            Url::generate(static::ROUTE_PUNCHOUT_CATALOG_CONNECTION_ACTIVATE, [
260
                static::URL_PARAM_ID_PUNCHOUT_CATALOG_CONNECTION => $punchoutCatalogConnection->getIdPunchoutCatalogConnection(),
261
            ]),
262
            'Activate'
263
        );
264
    }
265
266
    /**
267
     * @param \Orm\Zed\PunchoutCatalog\Persistence\PgwPunchoutCatalogConnection $punchoutCatalogConnection
268
     *
269
     * @return string
270
     */
271
    protected function getStatusLabel(PgwPunchoutCatalogConnection $punchoutCatalogConnection): string
272
    {
273
        if (!$punchoutCatalogConnection->getIsActive()) {
274
            return $this->generateLabel(static::STATUS_INACTIVE, 'label-danger');
275
        }
276
277
        return $this->generateLabel(static::STATUS_ACTIVE, 'label-info');
278
    }
279
}
280