Completed
Pull Request — master (#594)
by Amrouche
03:41
created

AuditItemDataProvider::getItem()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 10
rs 9.4285
cc 2
eloc 5
nc 2
nop 4
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
namespace ApiPlatform\Core\Bridge\SimpleThingsEntityAudit\Doctrine\Orm;
13
14
use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\QueryItemExtensionInterface;
15
use ApiPlatform\Core\Bridge\Doctrine\Orm\ItemDataProvider;
16
use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
17
use ApiPlatform\Core\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
18
use Doctrine\Common\Persistence\ManagerRegistry;
19
use SimpleThings\EntityAudit\AuditManager;
20
use SimpleThings\EntityAudit\AuditReader;
21
22
/**
23
 * Item data provider for the Doctrine ORM with the entity Audit Extension.
24
 *
25
 * @author Amrouche Hamza <[email protected]>
26
 */
27
class AuditItemDataProvider extends ItemDataProvider
28
{
29
    private $auditManager;
30
    private $auditReader;
31
32
    /**
33
     * @param ManagerRegistry                        $managerRegistry
34
     * @param PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory
35
     * @param PropertyMetadataFactoryInterface       $propertyMetadataFactory
36
     * @param QueryItemExtensionInterface[]          $itemExtensions
37
     * @param AuditReader                            $auditReader
38
     * @param AuditManager                           $auditManager
39
     */
40
    public function __construct(ManagerRegistry $managerRegistry, PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, array $itemExtensions, AuditReader $auditReader, AuditManager $auditManager)
41
    {
42
        parent::__construct($managerRegistry, $propertyNameCollectionFactory, $propertyMetadataFactory, $itemExtensions);
43
        $this->auditReader = $auditReader;
44
        $this->auditManager = $auditManager;
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50
    public function getItem(string $resourceClass, $id, string $operationName = null, bool $fetchData = false)
51
    {
52
        $resourceClass = str_replace('Audit', '', $resourceClass);
53
        if (true === $this->auditManager->getMetadataFactory()->isAudited($resourceClass)
54
        ) {
55
            return $this->auditReader->find($resourceClass, $id, 100 /*configurable ?*/);
56
        }
57
58
        parent::getItem($resourceClass, $id, $operationName, $fetchData);
59
    }
60
}
61