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

AuditItemDataProvider   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getItem() 0 10 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
        $auditResourceClass = str_replace('Audit', '', $resourceClass);
45
46
        if (false !== strpos($resourceClass, 'Audit') && true === $this->auditManager->getMetadataFactory()->isAudited($auditResourceClass)
47
        ) {
48
            return $this->auditReader->find($auditResourceClass, $id, 100 /*configurable ?*/);
49
        }
50
        throw new ResourceClassNotSupportedException(sprintf('Resource class %s is not Audited', $resourceClass));
51
    }
52
}
53