GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

RevisionManager   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 119
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 5
dl 0
loc 119
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A listen() 0 16 1
A getRevisionsCollection() 0 18 2
A getRevisions() 0 17 3
A getRevision() 0 8 1
A getRevisionsCount() 0 8 1
A clearRevisions() 0 12 1
1
<?php
2
3
namespace Sokil\Mongo\Document;
4
5
use Sokil\Mongo\Document;
6
use Sokil\Mongo\Expression;
7
8
class RevisionManager
9
{
10
11
    /**
12
     * Suffix added to collection name to get name of revisions collection
13
     * @var string
14
     */
15
    const REVISION_COLLECTION_SUFFIX = '.revisions';
16
    
17
    /**
18
     *
19
     * @var \Sokil\Mongo\Document
20
     */
21
    private $document;
22
23
    private $revisionsCollection;
24
25
    public function __construct(Document $document = null)
26
    {
27
        $this->document = $document;
28
    }
29
30
    /**
31
     * Start listening for changes in document
32
     */
33
    public function listen()
34
    {
35
        $revisionsCollection = $this->getRevisionsCollection();
36
        $document = $this->document;
37
38
        $createRevisionCallback = function () use ($revisionsCollection, $document) {
39
            // create new revision
40
            $revisionsCollection
41
                ->createDocument()
42
                ->setDocumentData($document->getOriginalData())
43
                ->save();
44
        };
45
46
        $this->document->onBeforeUpdate($createRevisionCallback, PHP_INT_MAX);
47
        $this->document->onBeforeDelete($createRevisionCallback, PHP_INT_MAX);
48
    }
49
50
    /**
51
     * @return \Sokil\Mongo\Collection
52
     */
53
    private function getRevisionsCollection()
54
    {
55
        if ($this->revisionsCollection) {
56
            return $this->revisionsCollection;
57
        }
58
59
        $collection = $this->document->getCollection();
60
        $revisionsCollectionName = $collection->getName() . self::REVISION_COLLECTION_SUFFIX;
61
62
        $this->revisionsCollection = $collection
63
            ->getDatabase()
64
            ->map($revisionsCollectionName, array(
65
                'documentClass' => '\Sokil\Mongo\Revision',
66
            ))
67
            ->getCollection($revisionsCollectionName);
68
69
        return $this->revisionsCollection;
70
    }
71
72
    public function getRevisions($limit = null, $offset = null)
73
    {
74
        $cursor = $this
0 ignored issues
show
Documentation Bug introduced by
The method where does not exist on object<Sokil\Mongo\Cursor>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
75
            ->getRevisionsCollection()
76
            ->find()
77
            ->where('__documentId__', $this->document->getId());
78
79
        if ($limit) {
80
            $cursor->limit($limit);
81
        }
82
83
        if ($offset) {
84
            $cursor->skip($offset);
85
        }
86
87
        return $cursor->findAll();
88
    }
89
90
    /**
91
     * Get revision by id
92
     *
93
     * @param int|string|\MongoId $id
94
     * @return \Sokil\Mongo\Revision
95
     */
96
    public function getRevision($id)
97
    {
98
        return $this
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->getRevisionsColle...->byId($id)->findOne(); of type Sokil\Mongo\Document|array|null adds the type array to the return on line 98 which is incompatible with the return type documented by Sokil\Mongo\Document\RevisionManager::getRevision of type Sokil\Mongo\Revision|null.
Loading history...
Deprecated Code introduced by
The method Sokil\Mongo\Cursor::findOne() has been deprecated with message: since v.1.22.2. Use ::one instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
99
            ->getRevisionsCollection()
100
            ->find()
101
            ->byId($id)
102
            ->findOne();
103
    }
104
105
    public function getRevisionsCount()
106
    {
107
        return $this
0 ignored issues
show
Documentation Bug introduced by
The method where does not exist on object<Sokil\Mongo\Cursor>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
108
            ->getRevisionsCollection()
109
            ->find()
110
            ->where('__documentId__', $this->document->getId())
111
            ->count();
112
    }
113
114
    public function clearRevisions()
115
    {
116
        $documentId = $this->document->getId();
117
        $this
118
            ->getRevisionsCollection()
119
            ->batchDelete(function (Expression $expression) use ($documentId) {
120
                /* @var $expression \Sokil\Mongo\Expression */
121
                return $expression->where('__documentId__', $documentId);
122
            });
123
124
        return $this;
125
    }
126
}
127