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.
Completed
Push — develop ( 31696d...d014e0 )
by Stuart
05:40
created

UsingUsers::loadUsersFromFile()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 30
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 30
rs 8.439
cc 5
eloc 16
nc 4
nop 1
1
<?php
2
3
/**
4
 * Copyright (c) 2011-present Mediasift Ltd
5
 * All rights reserved.
6
 *
7
 * Redistribution and use in source and binary forms, with or without
8
 * modification, are permitted provided that the following conditions
9
 * are met:
10
 *
11
 *   * Redistributions of source code must retain the above copyright
12
 *     notice, this list of conditions and the following disclaimer.
13
 *
14
 *   * Redistributions in binary form must reproduce the above copyright
15
 *     notice, this list of conditions and the following disclaimer in
16
 *     the documentation and/or other materials provided with the
17
 *     distribution.
18
 *
19
 *   * Neither the names of the copyright holders nor the names of his
20
 *     contributors may be used to endorse or promote products derived
21
 *     from this software without specific prior written permission.
22
 *
23
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
26
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
27
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
28
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
29
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
33
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34
 * POSSIBILITY OF SUCH DAMAGE.
35
 *
36
 * @category  Libraries
37
 * @package   Storyplayer/Modules/Users
38
 * @author    Stuart Herbert <[email protected]>
39
 * @copyright 2011-present Mediasift Ltd www.datasift.com
40
 * @license   http://www.opensource.org/licenses/bsd-license.php  BSD License
41
 * @link      http://datasift.github.io/storyplayer
42
 */
43
44
namespace Storyplayer\SPv2\Modules\Users;
45
46
use DataSift\Stone\ObjectLib\BaseObject;
47
use Prose\Prose;
48
use Storyplayer\SPv2\Modules\Exceptions;
49
use Storyplayer\SPv2\Modules\Log;
50
51
/**
52
 * work with the library of test users
53
 *
54
 * @category  Libraries
55
 * @package   Storyplayer/Modules/Users
56
 * @author    Stuart Herbert <[email protected]>
57
 * @copyright 2011-present Mediasift Ltd www.datasift.com
58
 * @license   http://www.opensource.org/licenses/bsd-license.php  BSD License
59
 * @link      http://datasift.github.io/storyplayer
60
 */
61
class UsingUsers extends Prose
62
{
63
    /**
64
     * load test users from the given filename
65
     *
66
     * NOTES:
67
     *
68
     * - Storyplayer calls this for you when you use the --users switch
69
     *
70
     * - it is safe to call this yourself from a story if you want to load
71
     *   additional users for any reason. however, Storyplayer will not manage
72
     *   saving these users for you - you WILL have to do that yourself
73
     *
74
     * @param  string $filename
75
     *         the JSON file to load users from
76
     * @return \DataSift\Stone\ObjectLib\BaseObject
77
     */
78
    public function loadUsersFromFile($filename)
79
    {
80
        // what are we doing?
81
        $log = Log::usingLog()->startAction("load test users from '{$filename}'");
82
83
        // load the file
84
        $raw = @file_get_contents($filename);
85
        if (!$raw || empty($raw)) {
86
            throw Exceptions::newActionFailedException(__METHOD__, "cannot open file '{$filename}' or file is empty");
87
        }
88
        $plainUsers = json_decode($raw);
89
        if ($plainUsers === null) {
90
            throw Exceptions::newActionFailedException(__METHOD__, "file '{$filename}' contains invalid JSON");
91
        }
92
        if (!is_object($plainUsers)) {
93
            throw Exceptions::newActionFailedException(__METHOD__, "file '{$filename}' must contain a JSON object");
94
        }
95
96
        // merge these in with any users we have already loaded
97
        $users = new BaseObject;
98
        $users->mergeFrom($plainUsers);
99
100
        // remember what we've loaded
101
        $this->st->setTestUsers($users);
102
103
        // all done
104
        $count = count(get_object_vars($users));
105
        $log->endAction("loaded {$count} test user(s)");
106
        return $users;
107
    }
108
109
    /**
110
     * save test users to disk
111
     *
112
     * NOTES:
113
     *
114
     * - Storyplayer calls this for you when all tests have completed
115
     *
116
     * - if you've loaded test users yourself inside your test, you'll need
117
     *   to call this method to save those test users
118
     *
119
     * @param  \DataSift\Stone\ObjectLib\BaseObject $users
120
     *         the test users to save to disk
121
     * @param  string $filename
122
     *         the filename to save to
123
     * @return void
124
     */
125
    public function saveUsersToFile($users, $filename)
126
    {
127
        // what are we doing?
128
        $log = Log::usingLog()->startAction("save test users to file '{$filename}'");
129
130
        // save the contents
131
        file_put_contents($filename, json_encode($users, JSON_PRETTY_PRINT));
132
133
        // all done
134
        $count = count(get_object_vars($users));
135
        $log->endAction("saved {$count} test user(s)");
136
    }
137
138
    /**
139
     * tell Storyplayer that test users loaded via the --users switch must
140
     * not be saved back to disk when the stories are over
141
     *
142
     * NOTES:
143
     *
144
     * - Storyplayer calls this for you if you use the --read-only-users
145
     *   switch, or put 'moduleSettings.users.readOnly' in your test
146
     *   environment config
147
     *
148
     * - this setting has no effect if you call saveUsersToFile() manually
149
     */
150
    public function setUsersFileIsReadOnly()
151
    {
152
        // what are we doing?
153
        $log = Log::usingLog()->startAction("mark test users file as read-only");
154
155
        // track the state change
156
        $this->st->setTestUsersFileIsReadOnly(true);
157
158
        // all done
159
        $log->endAction();
160
    }
161
}