Completed
Pull Request — master (#594)
by Amrouche
06:50
created

AuditCollectionDataProvider::getCollection()   A

Complexity

Conditions 2
Paths 2

Size

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