Passed
Push — master ( 713f8f...96a84a )
by
unknown
18:54 queued 12:11
created

ChangePositionPaymentMethodCreator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 6
rs 10
1
<?php
2
3
/*
4
 * This file has been created by developers from BitBag.
5
 * Feel free to contact us once you face any issues or want to start
6
 * You can find more information about us on https://bitbag.io and write us
7
 * an email on [email protected].
8
 */
9
10
declare(strict_types=1);
11
12
namespace BitBag\SyliusMolliePlugin\Creator;
13
14
use BitBag\SyliusMolliePlugin\Entity\MollieGatewayConfigInterface;
15
use Doctrine\Common\Persistence\ObjectManager;
0 ignored issues
show
Bug introduced by
The type Doctrine\Common\Persistence\ObjectManager 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...
16
use Sylius\Component\Resource\Repository\RepositoryInterface;
17
use Symfony\Component\HttpFoundation\Request;
18
19
final class ChangePositionPaymentMethodCreator implements ChangePositionPaymentMethodCreatorInterface
20
{
21
    /** @var RepositoryInterface */
22
    private $mollieGatewayRepository;
23
24
    /** @var ObjectManager */
25
    private $mollieGatewayObjectManager;
26
27
    public function __construct(
28
        RepositoryInterface $mollieGatewayRepository,
29
        ObjectManager $mollieGatewayObjectManager
30
    ) {
31
        $this->mollieGatewayRepository = $mollieGatewayRepository;
32
        $this->mollieGatewayObjectManager = $mollieGatewayObjectManager;
33
    }
34
35
    public function createFromRequest(Request $request): void
36
    {
37
        $positions = $this->emptyPositionFilter($request->get('data', []));
38
39
        foreach ($positions as $position) {
40
            $method = $this->mollieGatewayRepository->findOneBy(['methodId' => $position['name']]);
41
            if ($method instanceof MollieGatewayConfigInterface && isset($position['id'])) {
42
                $method->setPosition((int) $position['id']);
43
44
                $this->mollieGatewayObjectManager->persist($method);
45
            }
46
        }
47
48
        $this->mollieGatewayObjectManager->flush();
49
    }
50
51
    private function emptyPositionFilter(array $positions): array
52
    {
53
        return array_filter($positions, function (array $position): bool {
54
            return isset($position['id']) && $position['id'] !== '';
55
        });
56
    }
57
}
58