Completed
Pull Request — master (#594)
by Amrouche
06:50
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\DataProvider\ItemDataProviderInterface;
17
use ApiPlatform\Core\Exception\InvalidArgumentException;
18
use ApiPlatform\Core\Exception\ResourceClassNotSupportedException;
19
use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
20
use ApiPlatform\Core\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
21
use Doctrine\Common\Persistence\ManagerRegistry;
22
use Doctrine\ORM\EntityManagerInterface;
23
use Doctrine\ORM\QueryBuilder;
24
use SimpleThings\EntityAudit\AuditManager;
25
use SimpleThings\EntityAudit\AuditReader;
26
27
/**
28
 * Item data provider for the Doctrine ORM with the entity Audit Extension.
29
 *
30
 * @author Amrouche Hamza <[email protected]>
31
 */
32
class AuditItemDataProvider extends ItemDataProvider
33
{
34
    private $auditManager;
35
    private $auditReader;
36
37
    /**
38
     * @param ManagerRegistry                        $managerRegistry
39
     * @param PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory
40
     * @param PropertyMetadataFactoryInterface       $propertyMetadataFactory
41
     * @param QueryItemExtensionInterface[]          $itemExtensions
42
     * @param AuditReader $auditReader
43
     * @param AuditManager $auditManager
44
     */
45
    public function __construct(ManagerRegistry $managerRegistry, PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, array $itemExtensions = [], AuditReader $auditReader, AuditManager $auditManager)
46
    {
47
        parent::__construct($managerRegistry,$propertyNameCollectionFactory,$propertyMetadataFactory, $itemExtensions);
48
        $this->auditReader = $auditReader;
49
        $this->auditManager = $auditManager;
50
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function getItem(string $resourceClass, $id, string $operationName = null, bool $fetchData = false)
56
    {
57
        $resourceClass = str_replace('Audit', '', $resourceClass);
58
        if (true === $this->auditManager->getMetadataFactory()->isAudited($resourceClass)
59
        ) {
60
            return $this->auditReader->find($resourceClass, $id, 100 /*configurable ?*/);
61
        }
62
        
63
       parent::getItem($resourceClass, $id, $operationName, $fetchData);
64
    }
65
}
66