Passed
Pull Request — master (#2262)
by GRASSIOT
03:12
created

TraceableChainDataPersister   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 12
eloc 23
dl 0
loc 63
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 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
final 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
            $this->persisters = $dataPersister->persisters;
33
        }
34
    }
35
36
    public function getPersistersResponse(): array
37
    {
38
        return $this->persistersResponse;
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function supports($data): bool
45
    {
46
        return $this->decorated->supports($data);
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function persist($data)
53
    {
54
        foreach ($this->persisters as $persister) {
55
            $this->persistersResponse[\get_class($persister)] = null;
56
        }
57
        foreach ($this->persisters as $persister) {
58
            if ($persister->supports($data)) {
59
                $this->persistersResponse[\get_class($persister)] = true;
60
61
                return $persister->persist($data) ?? $data;
62
            }
63
64
            $this->persistersResponse[\get_class($persister)] = false;
65
        }
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71
    public function remove($data)
72
    {
73
        foreach ($this->persisters as $persister) {
74
            $this->persistersResponse[\get_class($persister)] = null;
75
        }
76
77
        foreach ($this->persisters as $persister) {
78
            if ($persister->supports($data)) {
79
                $this->persistersResponse[\get_class($persister)] = true;
80
81
                return $persister->remove($data);
82
            }
83
84
            $this->persistersResponse[\get_class($persister)] = false;
85
        }
86
    }
87
}
88