Passed
Pull Request — master (#2262)
by GRASSIOT
02:48
created

TraceableChainDataPersister   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 12
eloc 25
dl 0
loc 65
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A getPersistersResponse() 0 3 1
A remove() 0 14 4
A persist() 0 13 4
A supports() 0 3 1
1
<?php
2
3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace ApiPlatform\Core\Bridge\Symfony\Bundle\DataPersister;
15
16
use ApiPlatform\Core\DataPersister\ChainDataPersister;
17
use ApiPlatform\Core\DataPersister\DataPersisterInterface;
18
19
/**
20
 * @author Anthony GRASSIOT <[email protected]>
21
 */
22
class TraceableChainDataPersister implements DataPersisterInterface
23
{
24
    private $persisters = [];
25
    private $persistersResponse = [];
26
    private $decorated;
27
28
    public function __construct(DataPersisterInterface $dataPersister)
29
    {
30
        if ($dataPersister instanceof ChainDataPersister) {
31
            $this->decorated = $dataPersister;
32
            $reflection = new \ReflectionProperty(ChainDataPersister::class, 'persisters');
33
            $reflection->setAccessible(true);
34
            $this->persisters = $reflection->getValue($dataPersister);
35
        }
36
    }
37
38
    public function getPersistersResponse(): array
39
    {
40
        return $this->persistersResponse;
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function supports($data): bool
47
    {
48
        return $this->decorated->supports($data);
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function persist($data)
55
    {
56
        foreach ($this->persisters as $persister) {
57
            $this->persistersResponse[\get_class($persister)] = null;
58
        }
59
        foreach ($this->persisters as $persister) {
60
            if ($persister->supports($data)) {
61
                $this->persistersResponse[\get_class($persister)] = true;
62
63
                return $persister->persist($data) ?? $data;
64
            }
65
66
            $this->persistersResponse[\get_class($persister)] = false;
67
        }
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function remove($data)
74
    {
75
        foreach ($this->persisters as $persister) {
76
            $this->persistersResponse[\get_class($persister)] = null;
77
        }
78
79
        foreach ($this->persisters as $persister) {
80
            if ($persister->supports($data)) {
81
                $this->persistersResponse[\get_class($persister)] = true;
82
83
                return $persister->remove($data);
84
            }
85
86
            $this->persistersResponse[\get_class($persister)] = false;
87
        }
88
    }
89
}
90