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

AuditCollectionDataProvider   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 getCollection() 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\CollectionDataProviderInterface;
15
use ApiPlatform\Core\Exception\ResourceClassNotSupportedException;
16
use SimpleThings\EntityAudit\AuditManager;
17
use SimpleThings\EntityAudit\AuditReader;
18
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
19
20
/**
21
 * Collection data provider for the Doctrine ORM with the EntityAudit extension.
22
 *
23
 * @author Amrouche Hamza <[email protected]>
24
 */
25
class AuditCollectionDataProvider implements CollectionDataProviderInterface
26
{
27
    private $auditManager;
28
    private $auditReader;
29
30
    /**
31
     * @param AuditReader  $auditReader
32
     * @param AuditManager $auditManager
33
     */
34
    public function __construct(AuditReader $auditReader, AuditManager $auditManager)
35
    {
36
        $this->auditManager = $auditManager;
37
        $this->auditReader = $auditReader;
38
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function getCollection(string $resourceClass, string $operationName = null)
44
    {
45
        if ('audits' === $operationName && true === $this->auditManager->getMetadataFactory()->isAudited($resourceClass)) {
46
            throw new NotFoundHttpException(sprintf('Please add an {id} to get the revisions of %s', $resourceClass));
47
            // or delete this ? and use the itemDataProvider as an collection providers too since we need an id to get the revision
48
        }
49
        throw new ResourceClassNotSupportedException(sprintf('Resource class %s is not Audited', $resourceClass));
50
    }
51
}
52