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

AuditCollectionDataProvider   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 36
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 36
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A getCollection() 0 11 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