User::shutdown()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
namespace Agavi\User;
3
4
// +---------------------------------------------------------------------------+
5
// | This file is part of the Agavi package.                                   |
6
// | Copyright (c) 2005-2011 the Agavi Project.                                |
7
// | Based on the Mojavi3 MVC Framework, Copyright (c) 2003-2005 Sean Kerr.    |
8
// |                                                                           |
9
// | For the full copyright and license information, please view the LICENSE   |
10
// | file that was distributed with this source code. You can also view the    |
11
// | LICENSE file online at http://www.agavi.org/LICENSE.txt                   |
12
// |   vi: set noexpandtab:                                                    |
13
// |   Local Variables:                                                        |
14
// |   indent-tabs-mode: t                                                     |
15
// |   End:                                                                    |
16
// +---------------------------------------------------------------------------+
17
use Agavi\Core\Context;
18
use Agavi\Exception\InitializationException;
19
use Agavi\Util\AttributeHolder;
20
21
/**
22
 * AgaviUser wraps a client session and provides accessor methods for user
23
 * attributes. It also makes storing and retrieving multiple page form data
24
 * rather easy by allowing user attributes to be stored in namespaces, which
25
 * help organize data.
26
 *
27
 * @package    agavi
28
 * @subpackage user
29
 *
30
 * @author     Sean Kerr <[email protected]>
31
 * @copyright  Authors
32
 * @copyright  The Agavi Project
33
 *
34
 * @since      0.9.0
35
 *
36
 * @version    $Id$
37
 */
38
class User extends AttributeHolder
39
{
40
    /**
41
     * @var        Context A Context instance.
42
     */
43
    protected $context = null;
44
45
    /**
46
     * @var        string Storage namespace where user attributes are put.
47
     */
48
    protected $storageNamespace = 'org.agavi.user.User';
49
50
    /**
51
     * Retrieve the current application context.
52
     *
53
     * @return     Context A Context instance.
54
     *
55
     * @author     Sean Kerr <[email protected]>
56
     * @since      0.9.0
57
     */
58
    final public function getContext()
59
    {
60
        return $this->context;
61
    }
62
63
    /**
64
     * Retrieve the Storage namespace
65
     *
66
     * @return     string The Storage namespace
67
     *
68
     * @author     David Zülke <[email protected]>
69
     * @since      0.11.0
70
     */
71
    public function getStorageNamespace()
72
    {
73
        return $this->storageNamespace;
74
    }
75
76
    /**
77
     * Initialize this User.
78
     *
79
     * @param      Context $context An Context instance.
80
     * @param      array   $parameters An associative array of initialization parameters.
81
     *
82
     * @throws     InitializationException If an error occurs while
83
     *                                                 initializing this User.
84
     *
85
     * @author     Sean Kerr <[email protected]>
86
     * @author     David Zülke <[email protected]>
87
     * @since      0.9.0
88
     */
89
    public function initialize(Context $context, array $parameters = array())
90
    {
91
        $this->context = $context;
92
93
        if (isset($parameters['default_namespace'])) {
94
            $this->defaultNamespace = $parameters['default_namespace'];
95
        }
96
        
97
        if (isset($parameters['storage_namespace'])) {
98
            $this->storageNamespace = $parameters['storage_namespace'];
99
        }
100
101
        $this->setParameters($parameters);
102
        
103
        // read data from storage
104
        $this->attributes = $context->getStorage()->read($this->storageNamespace);
0 ignored issues
show
Documentation Bug introduced by
It seems like $context->getStorage()->...this->storageNamespace) of type * is incompatible with the declared type array of property $attributes.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
105
106
        if ($this->attributes == null) {
107
            // initialize our attributes array
108
            $this->attributes = array();
109
        }
110
    }
111
112
    /**
113
     * Startup the user.
114
     *
115
     * You'd usually try to auth from a cookie here etc.
116
     *
117
     * @author     David Zülke <[email protected]>
118
     * @since      0.11.0
119
     */
120
    public function startup()
121
    {
122
    }
123
124
    /**
125
     * Execute the shutdown procedure.
126
     *
127
     * @author     Sean Kerr <[email protected]>
128
     * @since      0.9.0
129
     */
130
    public function shutdown()
131
    {
132
        // write attributes to the storage
133
        $this->getContext()->getStorage()->write($this->storageNamespace, $this->attributes);
134
    }
135
}
136