Completed
Push — master ( fb0873...0bfc19 )
by Julián
02:05
created

DocumentManager::getDefaultRepositoryClassName()   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
nc 1
cc 1
eloc 2
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
21
/**
22
 * Custom Doctrine CouchDB Document Manager.
23
 */
24
class DocumentManager extends BaseDocumentManager
25
{
26
    /**
27
     * Repository factory.
28
     *
29
     * @var DefaultRepositoryFactory
30
     */
31
    protected $repositoryFactory;
32
33
    /**
34
     * Default repository class name.
35
     *
36
     * @var string
37
     */
38
    protected $repositoryClassName = 'Doctrine\ODM\CouchDB\DocumentRepository';
39
40
    /**
41
     * Get repository factory.
42
     *
43
     * @return DefaultRepositoryFactory
44
     */
45
    public function getRepositoryFactory()
46
    {
47
        if ($this->repositoryFactory === null) {
48
            $this->repositoryFactory = new DefaultRepositoryFactory();
49
        }
50
51
        return $this->repositoryFactory;
52
    }
53
54
    /**
55
     * Set repository factory.
56
     *
57
     * @param DefaultRepositoryFactory $repositoryFactory
58
     */
59
    public function setRepositoryFactory(DefaultRepositoryFactory $repositoryFactory)
60
    {
61
        $this->repositoryFactory = $repositoryFactory;
62
    }
63
64
    /**
65
     * Get default repository class name.
66
     *
67
     * @return string
68
     */
69
    public function getDefaultRepositoryClassName()
70
    {
71
        return $this->repositoryClassName;
72
    }
73
74
    /**
75
     * Set default repository class name.
76
     *
77
     * @param string $className
78
     *
79
     * @throws \InvalidArgumentException
80
     */
81
    public function setDefaultRepositoryClassName($className)
82
    {
83
        $reflectionClass = new \ReflectionClass($className);
84
85
        if (!$reflectionClass->implementsInterface(ObjectRepository::class)) {
86
            throw new \InvalidArgumentException(sprintf(
87
                'Invalid repository class "%s". It must be a Doctrine\Common\Persistence\ObjectRepository.',
88
                $className
89
            ));
90
        }
91
92
        $this->repositoryClassName = $className;
93
    }
94
95
    /**
96
     * Gets the repository for a class.
97
     *
98
     * @param string $documentName
99
     *
100
     * @return \Doctrine\ODM\CouchDB\DocumentRepository
101
     */
102
    public function getRepository($documentName)
103
    {
104
        return $this->getRepositoryFactory()->getRepository($this, $documentName);
105
    }
106
107
    /**
108
     * {@inheritdoc}
109
     */
110
    public static function create($couchParams, Configuration $config = null, EventManager $evm = null)
111
    {
112
        if (is_array($couchParams)) {
113
            $couchClient = CouchDBClient::create($couchParams);
114
        } elseif ($couchParams instanceof CouchDBClient) {
115
            $couchClient = $couchParams;
116
        } else {
117
            throw new \InvalidArgumentException('Expecting array of instance of CouchDBClient as first argument to DocumentManager::create().');
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 144 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
118
        }
119
120
        return new static($couchClient, $config, $evm);
121
    }
122
}
123