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

AuditItemDataProvider   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 2
Bugs 0 Features 2
Metric Value
wmc 4
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 3
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\DataProvider\ItemDataProviderInterface;
15
use ApiPlatform\Core\Exception\ResourceClassNotSupportedException;
16
use SimpleThings\EntityAudit\AuditManager;
17
use SimpleThings\EntityAudit\AuditReader;
18
19
/**
20
 * Item data provider for the Doctrine ORM with the entity Audit Extension.
21
 *
22
 * @author Amrouche Hamza <[email protected]>
23
 */
24
class AuditItemDataProvider implements ItemDataProviderInterface
25
{
26
    private $auditManager;
27
    private $auditReader;
28
29
    /**
30
     * @param AuditReader  $auditReader
31
     * @param AuditManager $auditManager
32
     */
33
    public function __construct(AuditReader $auditReader, AuditManager $auditManager)
34
    {
35
        $this->auditReader = $auditReader;
36
        $this->auditManager = $auditManager;
37
    }
38
39
    /**
40
     * {@inheritdoc}
41
     */
42
    public function getItem(string $resourceClass, $id, string $operationName = null, bool $fetchData = false)
43
    {
44
        if ('audits' === $operationName && true === $this->auditManager->getMetadataFactory()->isAudited($resourceClass)
45
        ) {
46
            return $this->auditReader->find($resourceClass, $id, 100 /*configurable ?*/);
47
        }
48
        throw new ResourceClassNotSupportedException(sprintf('Resource class %s is not Audited', $resourceClass));
49
    }
50
}
51