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

ActivityRepository::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
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