UserContext::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 3
cts 3
cp 1
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * zf2-featureflags.
4
 *
5
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
6
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
7
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
8
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
9
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
10
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
11
 * SOFTWARE.
12
 *
13
 * @copyright 2016 MehrAlsNix (http://www.mehralsnix.de)
14
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
15
 *
16
 * @link      http://github.com/MehrAlsNix/zf2-featureflags
17
 */
18
19
namespace MehrAlsNix\FeatureToggle\Context;
20
21
use Qandidate\Toggle\Context;
22
use Qandidate\Toggle\ContextFactory;
23
use Zend\Authentication\AuthenticationServiceInterface;
24
25
/**
26
 * Context factory is implemented in an application to provide the context for
27
 * feature flipping.
28
 *
29
 * To abstract away the business object from the feature library, the
30
 * application is responsible for mapping the business objects into a context
31
 * based for feature flipping. For example:
32
 *
33
 *     $request = ...;
34
 *     $user    = $repository->findBy(..);
35
 *     $context = new Context();
36
 *     $context->set('user_id', $user->getId());
37
 *     $context->set('company_id', $user->getCompanyId());
38
 *     $context->set('ip', $request->getClientIp());
39
 */
40
class UserContext extends ContextFactory
41
{
42
    /** @var AuthenticationServiceInterface $tokenStorage */
43
    private $tokenStorage;
44
45 1
    public function __construct(AuthenticationServiceInterface $storage)
46
    {
47 1
        $this->tokenStorage = $storage;
48 1
    }
49
50
    /**
51
     * @return Context
52
     */
53 1
    public function createContext()
54
    {
55 1
        $context = new Context();
56 1
        $token = $this->tokenStorage->getIdentity();
57 1
        if (null !== $token) {
58
            $context->set('username', $this->tokenStorage->getIdentity());
59
        }
60 1
        return $context;
61
    }
62
}
63