Completed
Push — master ( 81b65f...3c6407 )
by vistart
07:48
created

BaseMongoMessageQuery::recipients()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 5
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
crap 6
1
<?php
2
3
/**
4
 *   _   __ __ _____ _____ ___  ____  _____
5
 *  | | / // // ___//_  _//   ||  __||_   _|
6
 *  | |/ // /(__  )  / / / /| || |     | |
7
 *  |___//_//____/  /_/ /_/ |_||_|     |_|
8
 * @link https://vistart.me/
9
 * @copyright Copyright (c) 2016 - 2017 vistart
10
 * @license https://vistart.me/license/
11
 */
12
13
namespace rhosocial\base\models\queries;
14
15
use rhosocial\base\models\models\BaseMongoEntityModel;
16
use rhosocial\base\models\traits\MessageQueryTrait;
17
18
/**
19
 * @version 1.0
20
 * @author vistart <[email protected]>
21
 */
22
class BaseMongoMessageQuery extends BaseMongoBlameableQuery
23
{
24
    use MessageQueryTrait;
25
26
    /**
27
     * Get the opposite relation.
28
     * @param BaseUserModel|string $user initiator
29
     * @param BaseUserModel|string $other recipient.
30
     * @param Connection $database
31
     * @return {$model->class}
0 ignored issues
show
Documentation introduced by
The doc-type {$model->class} could not be parsed: Unknown type name "{$model-" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
32
     */
33
    public function opposite($user, $other, $database = null)
34
    {
35
        $model = $this->noInitModel;
36
        return $this->andWhere(
37
            [$model->createdByAttribute => BaseMongoEntityModel::compositeGUIDs($other),
38
        $model->otherGuidAttribute => BaseMongoEntityModel::compositeGUIDs($user)])->one($database);
39
    }
40
41
    /**
42
     * Get all the opposites.
43
     * @param string $user initator.
44
     * @param array $others all recipients.
45
     * @param Connection $database
46
     * @return array instances.
47
     */
48
    public function opposites($user, $others = [], $database = null)
49
    {
50
        $model = $this->noInitModel;
51
        $query = $this->andWhere([$model->otherGuidAttribute => BaseMongoEntityModel::compositeGUIDs($user)]);
52
        /* @var $query static */
53
        if (!empty($others)) {
54
            $query = $query->andWhere([$model->createdByAttribute => BaseMongoEntityModel::compositeGUIDs($others)]);
55
        }
56
        return $query->all($database);
57
    }
58
59
    /**
60
     * Specify initiators.
61
     * @param string|array $users the guid of initiator if string, or guid array
62
     * of initiators if array.
63
     * @return static $this
64
     */
65
    public function initiators($users = [])
66
    {
67
        if (empty($users)) {
68
            return $this;
69
        }
70
        $model = $this->noInitModel;
71
        return $this->andWhere([$model->createdByAttribute => BaseMongoEntityModel::compositeGUIDs($users)]);
72
    }
73
74
    /**
75
     * Specify recipients.
76
     * @param string|array $users the guid of recipient if string, or guid array
77
     * of recipients if array.
78
     * @return static $this
79
     */
80
    public function recipients($users = [])
81
    {
82
        if (empty($users)) {
83
            return $this;
84
        }
85
        $model = $this->noInitModel;
86
        return $this->andWhere([$model->otherGuidAttribute => BaseMongoEntityModel::compositeGUIDs($users)]);
87
    }
88
}