Completed
Push — master ( 36630e...8a890e )
by Jonathan
08:38 queued 04:30
created

Doctrine2::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 7
ccs 6
cts 6
cp 1
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 5
nc 1
nop 3
crap 1
1
<?php
2
declare(strict_types=1);
3
/**
4
 * Caridea
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
7
 * use this file except in compliance with the License. You may obtain a copy of
8
 * the License at
9
 *
10
 * http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
14
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
15
 * License for the specific language governing permissions and limitations under
16
 * the License.
17
 *
18
 * @copyright 2015-2016 LibreWorks contributors
19
 * @license   http://opensource.org/licenses/Apache-2.0 Apache 2.0 License
20
 */
21
namespace Caridea\Dao;
22
23
use Doctrine\ORM\EntityManager;
24
25
/**
26
 * An abstract Data Access Object for MongoDb.
27
 *
28
 * Requires the `mongodb` extension.
29
 *
30
 * @copyright 2015-2016 LibreWorks contributors
31
 * @license   http://opensource.org/licenses/Apache-2.0 Apache 2.0 License
32
 */
33
abstract class Doctrine2 extends Dao
34
{
35
    /**
36
     * @var \Doctrine\ORM\EntityManager The entity manager
37
     */
38
    protected $manager;
39
    /**
40
     * @var string
41
     */
42
    protected $entityName;
43
    /**
44
     *
45
     * @var \Doctrine\ORM\EntityRepository The entity repository
46
     */
47
    protected $repository;
48
49
    /**
50
     * Creates a new MongoDB DAO.
51
     *
52
     * @param \Doctrine\ORM\EntityManager $manager The entity manager
53
     * @param string $entityName The entity name
54
     * @param \Psr\Log\LoggerInterface $logger A logger
55
     */
56 4
    public function __construct(EntityManager $manager, string $entityName, \Psr\Log\LoggerInterface $logger = null)
57
    {
58 4
        parent::__construct($logger);
59 4
        $this->manager = $manager;
60 4
        $this->entityName = $entityName;
61 4
        $this->repository = $manager->getRepository($entityName);
62 4
    }
63
64
    /**
65
     * Executes something in the context of the entityManager.
66
     *
67
     * Exceptions are caught and translated.
68
     *
69
     * @param Closure $cb The closure to execute, takes the entityManager
70
     * @return mixed whatever the function returns, this method also returns
71
     * @throws \Caridea\Dao\Exception If a database problem occurs
72
     * @see \Caridea\Dao\Exception\Translator\Doctrine
73
     */
74 2
    protected function doExecute(\Closure $cb)
75
    {
76
        try {
77 2
            return $cb($this->manager);
78 1
        } catch (\Exception $e) {
79 1
            throw Exception\Translator\Doctrine::translate($e);
80
        }
81
    }
82
83
    /**
84
     * Executes something in the context of the entityRepository.
85
     *
86
     * Exceptions are caught and translated.
87
     *
88
     * @param Closure $cb The closure to execute, takes the entityRepository
89
     * @return mixed whatever the function returns, this method also returns
90
     * @throws \Caridea\Dao\Exception If a database problem occurs
91
     * @see \Caridea\Dao\Exception\Translator\Doctrine
92
     */
93 2
    protected function doExecuteInRepository(\Closure $cb)
94
    {
95
        try {
96 2
            return $cb($this->repository);
97 1
        } catch (\Exception $e) {
98 1
            throw Exception\Translator\Doctrine::translate($e);
99
        }
100
    }
101
}
102