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

AuditCollectionDataProvider   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A getCollection() 0 10 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\CollectionDataProvider;
15
use ApiPlatform\Core\Bridge\Doctrine\Orm\Extension\QueryCollectionExtensionInterface;
16
use Doctrine\Common\Persistence\ManagerRegistry;
17
use SimpleThings\EntityAudit\AuditManager;
18
use SimpleThings\EntityAudit\AuditReader;
19
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
20
21
/**
22
 * Collection data provider for the Doctrine ORM with the EntityAudit extension.
23
 *
24
 * @author Amrouche Hamza <[email protected]>
25
 */
26
class AuditCollectionDataProvider extends CollectionDataProvider
27
{
28
    private $auditManager;
29
    private $auditReader;
30
31
    /**
32
     * @param ManagerRegistry                     $managerRegistry
33
     * @param QueryCollectionExtensionInterface[] $collectionExtensions
34
     * @param AuditReader                         $auditReader
35
     * @param AuditManager                        $auditManager
36
     */
37
    public function __construct(ManagerRegistry $managerRegistry, array $collectionExtensions, AuditReader $auditReader, AuditManager $auditManager)
38
    {
39
        parent::__construct($managerRegistry, $collectionExtensions);
40
        $this->auditManager = $auditManager;
41
        $this->auditReader = $auditReader;
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function getCollection(string $resourceClass, string $operationName = null)
48
    {
49
        $resourceClass = str_replace('Audit', '', $resourceClass);
50
        if (true === $this->auditManager->getMetadataFactory()->isAudited($resourceClass)
51
        ) {
52
            throw new NotFoundHttpException(sprintf('Please add an {id} to get the revisions of %s', $resourceClass));
53
        }
54
55
        return parent::getCollection($resourceClass, $operationName);
56
    }
57
}
58