Completed
Pull Request — master (#8)
by
unknown
04:37
created

ActivityRepository   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 4
dl 0
loc 34
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A findActivityById() 0 21 3
1
<?php
2
3
/*
4
 * This file is part of the xAPI package.
5
 *
6
 * (c) Christian Flothmann <[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 XApi\Repository\Doctrine\Repository;
13
14
use Xabbuh\XApi\Common\Exception\NotFoundException;
15
use Xabbuh\XApi\Model\Activity;
16
use Xabbuh\XApi\Model\IRI;
17
use XApi\Repository\Api\ActivityRepositoryInterface;
18
use XApi\Repository\Doctrine\Mapping\Object as MappedObject;
19
use XApi\Repository\Doctrine\Storage\ObjectStorage;
20
21
/**
22
 * Doctrine based {@link Activity} repository.
23
 *
24
 * @author Jérôme Parmentier <[email protected]>
25
 */
26
final class ActivityRepository implements ActivityRepositoryInterface
27
{
28
    private $storage;
29
30
    public function __construct(ObjectStorage $storage)
31
    {
32
        $this->storage = $storage;
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function findActivityById(IRI $activityId)
39
    {
40
        $criteria = array(
41
            'type' => MappedObject::TYPE_ACTIVITY,
42
            'activityId' => $activityId->getValue(),
43
        );
44
45
        $mappedObject = $this->storage->findObject($criteria);
46
47
        if (null === $mappedObject) {
48
            throw new NotFoundException(sprintf('No activity could be found matching the ID "%s".', $activityId->getValue()));
49
        }
50
51
        $activity = $mappedObject->getModel();
52
53
        if (!$activity instanceof Activity) {
54
            throw new NotFoundException(sprintf('No activity could be found matching the ID "%s".', $activityId->getValue()));
55
        }
56
57
        return $activity;
58
    }
59
}
60