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

AuditItemDataProvider   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 2
Bugs 0 Features 2
Metric Value
wmc 3
c 2
b 0
f 2
lcom 1
cbo 4
dl 0
loc 27
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getItem() 0 8 2
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\ResourceClassNotFoundException;
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 SimpleThings\EntityAudit\AuditManager;
23
use SimpleThings\EntityAudit\AuditReader;
24
25
/**
26
 * Item data provider for the Doctrine ORM with the entity Audit Extension.
27
 *
28
 * @author Amrouche Hamza <[email protected]>
29
 */
30
class AuditItemDataProvider implements ItemDataProviderInterface
31
{
32
    private $auditManager;
33
    private $auditReader;
34
35
    /**
36
     * @param AuditReader                            $auditReader
37
     * @param AuditManager                           $auditManager
38
     */
39
    public function __construct(AuditReader $auditReader, AuditManager $auditManager)
40
    {
41
        $this->auditReader = $auditReader;
42
        $this->auditManager = $auditManager;
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function getItem(string $resourceClass, $id, string $operationName = null, bool $fetchData = false)
49
    {
50
        if (true === $this->auditManager->getMetadataFactory()->isAudited($resourceClass)
51
        ) {
52
            return $this->auditReader->find($resourceClass, $id, 100 /*configurable ?*/);
53
        }
54
        throw new ResourceClassNotSupportedException(sprintf('Resource class %s is not Audited', $resourceClass));
55
    }
56
}
57