Completed
Push — master ( c2be97...312fc5 )
by Stephen
02:08
created

Accounts::findPosts()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 3
1
<?php
2
3
namespace StarCitizen\Accounts;
4
5
use StarCitizen\Base\StarCitizenAbstract;
6
use StarCitizen\Models\Posts;
7
use StarCitizen\Models\Profile;
8
use StarCitizen\Models\Threads;
9
10
/**
11
 * Class Accounts
12
 *
13
 * @package StarCitizen\Accounts;
14
 */
15
class Accounts extends StarCitizenAbstract
16
{
17
18
    /**
19
     * @var string
20
     */
21
    protected static $system = "accounts";
22
23
    /**
24
     * Constants Profile Types
25
     */
26
    const DOSSIER = "dossier";
27
    const FORUM = "forum_profile";
28
    const FULL = "full_profile";
29
    const THREADS = "threads";
30
    const POSTS = "posts";
31
    const MEMBERSHIPS = "memberships";
32
33
    /**
34
     * Model Map
35
     */
36
    const MODELS = [
37
        Accounts::DOSSIER => '\Profile',
38
        Accounts::FORUM => '\Profile',
39
        Accounts::FULL => '\Profile',
40
        Accounts::THREADS => '\Threads',
41
        Accounts::POSTS => '\Posts',
42
        Accounts::MEMBERSHIPS => '',
43
    ];
44
45
    /**
46
     * Find an account information
47
     *
48
     * @param $id
49
     * @param string $profileType
50
     * @param bool $cache
51
     * @param bool $raw
52
     *
53
     * @return bool|mixed
54
     */
55
    private static function find($id, $profileType = Accounts::FULL, $cache = false, $raw = false)
56
    {
57
        $profileType = ($cache === true)? Accounts::FULL : $profileType;
58
        $cache = ($cache === true)? "cache" : "live";
59
60
        $params = [
61
            'api_source' => $cache,
62
            'system' => self::$system,
63
            'action' => $profileType,
64
            'target_id' => $id,
65
            'expedite' => '0',
66
            'format' => 'json'
67
        ];
68
69
        $response = json_decode(self::$client->getResult($params)->getBody()->getContents(), true);
70
        if ($response['request_stats']['query_status'] == "success")
71
            if ($raw === true)
72
                return $response;
73
            else
74
                return self::fillModel($profileType, $response['data']);
75
76
        return false;
77
    }
78
79
    /**
80
     * @param $id
81
     * @param bool $cache
82
     * @param bool $raw
83
     *
84
     * @return bool|Profile|string
85
     */
86
    protected static function findProfile($id, $cache = false, $raw = false)
87
    {
88
        return self::find($id, Accounts::FULL, $cache, $raw);
89
    }
90
91
    /**
92
     * @param $id
93
     * @param bool $cache
94
     * @param bool $raw
95
     *
96
     * @return bool|Threads|string
97
     */
98
    protected static function findThreads($id, $cache = false, $raw = false)
99
    {
100
        return Accounts::find($id, Accounts::THREADS, $cache, $raw);
101
    }
102
103
    /**
104
     * @param $id
105
     * @param bool $cache
106
     * @param bool $raw
107
     *
108
     * @return bool|Posts|string
109
     */
110
    protected static function findPosts($id, $cache = false, $raw = false)
111
    {
112
        return Accounts::find($id, Accounts::POSTS, $cache, $raw);
113
    }
114
}