Completed
Pull Request — master (#399)
by Christian
03:10
created

ServiceContainer::onAutoCategoryResolver()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 0
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * (c) shopware AG <[email protected]>
4
 * For the full copyright and license information, please view the LICENSE
5
 * file that was distributed with this source code.
6
 */
7
8
namespace ShopwarePlugins\Connect\Subscribers;
9
10
use Enlight\Event\SubscriberInterface;
11
use Shopware\Components\Model\ModelManager;
12
use Shopware\Connect\Gateway\PDO;
13
use Shopware\CustomModels\Connect\Attribute;
14
use Shopware\CustomModels\Connect\PaymentRepository;
15
use Shopware\CustomModels\Connect\ProductToRemoteCategory;
16
use Shopware\CustomModels\Connect\RemoteCategory;
17
use ShopwarePlugins\Connect\Components\Api\Request\RestApiRequest;
18
use ShopwarePlugins\Connect\Components\CategoryExtractor;
19
use ShopwarePlugins\Connect\Components\CategoryResolver\AutoCategoryResolver;
20
use ShopwarePlugins\Connect\Components\Config;
21
use ShopwarePlugins\Connect\Components\CategoryResolver\DefaultCategoryResolver;
22
use ShopwarePlugins\Connect\Components\ConfigFactory;
23
use ShopwarePlugins\Connect\Components\ConnectExport;
24
use ShopwarePlugins\Connect\Components\ConnectFactory;
25
use ShopwarePlugins\Connect\Components\ErrorHandler;
26
use ShopwarePlugins\Connect\Components\FrontendQuery\FrontendQuery;
27
use ShopwarePlugins\Connect\Components\ImportService;
28
use ShopwarePlugins\Connect\Components\ProductStream\ProductSearch;
29
use ShopwarePlugins\Connect\Components\ProductStream\ProductStreamRepository;
30
use ShopwarePlugins\Connect\Components\ProductStream\ProductStreamService;
31
use Shopware\CustomModels\Connect\ProductStreamAttributeRepository;
32
use ShopwarePlugins\Connect\Components\RandomStringGenerator;
33
use ShopwarePlugins\Connect\Components\Validator\ProductAttributesValidator\ProductsAttributesValidator;
34
use ShopwarePlugins\Connect\Services\ExportAssignmentService;
35
use ShopwarePlugins\Connect\Services\MenuService;
36
use ShopwarePlugins\Connect\Services\PaymentService;
37
use Shopware\Components\DependencyInjection\Container;
38
use Shopware\Models\Category\Category as CategoryModel;
39
use Shopware\Models\Article\Article as ArticleModel;
40
use Shopware\CustomModels\Connect\Attribute as ConnectAttribute;
41
use Enlight_Components_Db_Adapter_Pdo_Mysql;
42
use Shopware\CustomModels\Connect\ProductStreamAttribute;
43
44
class ServiceContainer implements SubscriberInterface
45
{
46
    /**
47
     * @var ModelManager
48
     */
49
    private $manager;
50
51
    /**
52
     * @var Enlight_Components_Db_Adapter_Pdo_Mysql
53
     */
54
    private $db;
55
56
    /**
57
     * @var Container
58
     */
59
    private $container;
60
61
    /** @var Config */
62
    private $config;
63
64
    /**
65
     * ServiceContainer constructor.
66
     * @param ModelManager $manager
67
     * @param Enlight_Components_Db_Adapter_Pdo_Mysql $db
68
     * @param Container $container
69
     * @param Config $config
70
     */
71
    public function __construct(
72
        ModelManager $manager,
73
        Enlight_Components_Db_Adapter_Pdo_Mysql $db,
74
        Container $container,
75
        Config $config
76
    ) {
77
        $this->manager = $manager;
78
        $this->db = $db;
79
        $this->container = $container;
80
        $this->config = $config;
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86
    public static function getSubscribedEvents()
87
    {
88
        return [
89
            'Enlight_Bootstrap_InitResource_swagconnect.product_stream_service' => 'onProductStreamService',
90
            'Enlight_Bootstrap_InitResource_swagconnect.product_search' => 'onProductSearch',
91
            'Enlight_Bootstrap_InitResource_swagconnect.payment_service' => 'onPaymentService',
92
            'Enlight_Bootstrap_InitResource_swagconnect.menu_service' => 'onMenuService',
93
            'Enlight_Bootstrap_InitResource_swagconnect.frontend_query' => 'onCreateFrontendQuery',
94
            'Enlight_Bootstrap_InitResource_swagconnect.rest_api_request' => 'onRestApiRequest',
95
            'Enlight_Bootstrap_InitResource_swagconnect.import_service' => 'onImportService',
96
            'Enlight_Bootstrap_InitResource_swagconnect.auto_category_reverter' => 'onAutoCategoryReverter',
97
            'Enlight_Bootstrap_InitResource_swagconnect.auto_category_resolver' => 'onAutoCategoryResolver',
98
            'Enlight_Bootstrap_InitResource_swagconnect.default_category_resolver' => 'onDefaultCategoryResolver',
99
            'Enlight_Bootstrap_InitResource_swagconnect.export_assignment_service' => 'onExportAssignmentService'
100
        ];
101
    }
102
103
    /**
104
     * @return ProductStreamService
105
     */
106
    public function onProductStreamService()
107
    {
108
        /** @var ProductStreamAttributeRepository $streamAttrRepository */
109
        $streamAttrRepository = $this->manager->getRepository(ProductStreamAttribute::class);
110
111
        return new ProductStreamService(
112
            new ProductStreamRepository($this->manager, $this->container->get('shopware_product_stream.repository')),
113
            $streamAttrRepository,
114
            $this->config
115
        );
116
    }
117
118
    /**
119
     * @return ProductSearch
120
     */
121
    public function onProductSearch()
122
    {
123
        return new ProductSearch(
124
            $this->container->get('shopware_product_stream.repository'),
125
            $this->config,
126
            $this->container->get('shopware_search.product_search'),
127
            $this->container->get('shopware_storefront.context_service')
128
        );
129
    }
130
131
    /**
132
     * @return MenuService
133
     */
134
    public function onMenuService()
135
    {
136
        return new MenuService(
137
            $this->container->get('shopware_plugininstaller.plugin_manager'),
138
            $this->manager
139
        );
140
    }
141
142
    /**
143
     * @return PaymentService
144
     */
145
    public function onPaymentService()
146
    {
147
        return new PaymentService(
148
            $this->manager->getRepository('Shopware\Models\Payment\Payment'),
149
            new PaymentRepository($this->manager)
150
        );
151
    }
152
153
    /**
154
     * @return FrontendQuery
155
     */
156
    public function onCreateFrontendQuery()
157
    {
158
        return new FrontendQuery($this->manager);
159
    }
160
161
    /**
162
     * @return RestApiRequest
163
     */
164
    public function onRestApiRequest()
165
    {
166
        return new RestApiRequest($this->config);
167
    }
168
169
    /**
170
     * @return \ShopwarePlugins\Connect\Components\ImportService
171
     */
172
    public function onImportService()
173
    {
174
        return new ImportService(
175
            $this->manager,
176
            $this->container->get('multi_edit.product'),
177
            $this->manager->getRepository(CategoryModel::class),
178
            $this->manager->getRepository(ArticleModel::class),
179
            $this->manager->getRepository(RemoteCategory::class),
180
            $this->manager->getRepository(ProductToRemoteCategory::class),
181
            $this->container->get('swagconnect.auto_category_resolver'),
182
            new CategoryExtractor(
183
                $this->manager->getRepository(ConnectAttribute::class),
184
                $this->container->get('swagconnect.auto_category_resolver'),
185
                new PDO($this->db->getConnection()),
186
                new RandomStringGenerator(),
187
                $this->db
188
            ),
189
            $this->container->get('CategoryDenormalization'),
190
            $this->container->get('shopware_attribute.data_persister')
191
        );
192
    }
193
194
    /**
195
     * @return \ShopwarePlugins\Connect\Components\AutoCategoryReverter
196
     */
197
    public function onAutoCategoryReverter()
198
    {
199
        return new \ShopwarePlugins\Connect\Components\AutoCategoryReverter(
200
            $this->container->get('swagconnect.import_service')
201
        );
202
    }
203
204
    /**
205
     * @return \ShopwarePlugins\Connect\Components\CategoryResolver\AutoCategoryResolver
206
     */
207
    public function onAutoCategoryResolver()
208
    {
209
        return new AutoCategoryResolver(
210
            $this->manager,
211
            $this->manager->getRepository(CategoryModel::class),
212
            $this->manager->getRepository(RemoteCategory::class),
213
            $this->config,
214
            $this->manager->getRepository(ProductToRemoteCategory::class)
215
        );
216
    }
217
218
    /**
219
     * @return ExportAssignmentService
220
     */
221
    public function onExportAssignmentService()
222
    {
223
        $connectFactory = new ConnectFactory();
224
225
        return new ExportAssignmentService(
226
          $this->manager->getRepository(Attribute::class),
227
          new ConnectExport(
228
              $connectFactory->getHelper(),
229
              $connectFactory->getSDK(),
230
              $this->manager,
231
              new ProductsAttributesValidator(),
232
              ConfigFactory::getConfigInstance(),
233
              new ErrorHandler(),
234
              $this->container->get('events')
235
          )
236
        );
237
    }
238
239
    /**
240
     * @return \ShopwarePlugins\Connect\Components\CategoryResolver\DefaultCategoryResolver
241
     */
242
    public function onDefaultCategoryResolver()
243
    {
244
        return new DefaultCategoryResolver(
245
            $this->manager,
246
            $this->manager->getRepository(RemoteCategory::class),
247
            $this->manager->getRepository(ProductToRemoteCategory::class),
248
            $this->manager->getRepository(CategoryModel::class)
249
        );
250
    }
251
}
252