DocumentManager::getDefaultRepositoryClassName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
/*
4
 * doctrine-manager-builder (https://github.com/juliangut/doctrine-manager-builder).
5
 * Doctrine2 managers builder.
6
 *
7
 * @license BSD-3-Clause
8
 * @link https://github.com/juliangut/doctrine-manager-builder
9
 * @author Julián Gutiérrez <[email protected]>
10
 */
11
12
namespace Jgut\Doctrine\ManagerBuilder\CouchDB;
13
14
use Doctrine\Common\EventManager;
15
use Doctrine\Common\Persistence\ObjectRepository;
16
use Doctrine\CouchDB\CouchDBClient;
17
use Doctrine\ODM\CouchDB\Configuration;
18
use Doctrine\ODM\CouchDB\DocumentManager as BaseDocumentManager;
19
use Jgut\Doctrine\ManagerBuilder\CouchDB\Repository\DefaultRepositoryFactory;
20
use Jgut\Doctrine\ManagerBuilder\CouchDB\Repository\RepositoryFactory;
21
22
/**
23
 * Custom Doctrine CouchDB Document Manager.
24
 */
25
class DocumentManager extends BaseDocumentManager
26
{
27
    /**
28
     * Repository factory.
29
     *
30
     * @var RepositoryFactory
31
     */
32
    protected $repositoryFactory;
33
34
    /**
35
     * Default repository class name.
36
     *
37
     * @var string
38
     */
39
    protected $repositoryClassName = 'Doctrine\ODM\CouchDB\DocumentRepository';
40
41
    /**
42
     * Get repository factory.
43
     *
44
     * @return DefaultRepositoryFactory
45
     */
46
    public function getRepositoryFactory()
47
    {
48
        if ($this->repositoryFactory === null) {
49
            $this->repositoryFactory = new DefaultRepositoryFactory();
50
        }
51
52
        return $this->repositoryFactory;
53
    }
54
55
    /**
56
     * Set repository factory.
57
     *
58
     * @param RepositoryFactory $repositoryFactory
59
     */
60
    public function setRepositoryFactory(RepositoryFactory $repositoryFactory)
61
    {
62
        $this->repositoryFactory = $repositoryFactory;
63
    }
64
65
    /**
66
     * Get default repository class name.
67
     *
68
     * @return string
69
     */
70
    public function getDefaultRepositoryClassName()
71
    {
72
        return $this->repositoryClassName;
73
    }
74
75
    /**
76
     * Set default repository class name.
77
     *
78
     * @param string $className
79
     *
80
     * @throws \InvalidArgumentException
81
     */
82
    public function setDefaultRepositoryClassName($className)
83
    {
84
        $reflectionClass = new \ReflectionClass($className);
85
86
        if (!$reflectionClass->implementsInterface(ObjectRepository::class)) {
87
            throw new \InvalidArgumentException(sprintf(
88
                'Invalid repository class "%s". It must be a Doctrine\Common\Persistence\ObjectRepository.',
89
                $className
90
            ));
91
        }
92
93
        $this->repositoryClassName = $className;
94
    }
95
96
    /**
97
     * Gets the repository for a class.
98
     *
99
     * @param string $documentName
100
     *
101
     * @return \Doctrine\ODM\CouchDB\DocumentRepository
102
     */
103
    public function getRepository($documentName)
104
    {
105
        return $this->getRepositoryFactory()->getRepository($this, $documentName);
106
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111
    public static function create($couchParams, Configuration $config = null, EventManager $evm = null)
112
    {
113
        if (is_array($couchParams)) {
114
            $couchClient = CouchDBClient::create($couchParams);
115
        } elseif ($couchParams instanceof CouchDBClient) {
116
            $couchClient = $couchParams;
117
        } else {
118
            throw new \InvalidArgumentException(
119
                'Expecting array of instance of CouchDBClient as first argument to DocumentManager::create().'
120
            );
121
        }
122
123
        return new static($couchClient, $config, $evm);
124
    }
125
}
126