Completed
Push — master ( 9450db...32d720 )
by Vojtěch
03:23
created

SessionProfileStorage::setProfile()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 8.5906
c 0
b 0
f 0
cc 5
eloc 15
nc 9
nop 1
1
<?php
2
3
namespace SixtyEightPublishers\Application\Storage;
4
5
use Nette\Http\Session;
6
use Nette\SmartObject;
7
use SixtyEightPublishers\Application\ActiveProfile;
8
use SixtyEightPublishers\Application\IProfile;
9
use SixtyEightPublishers\Application\IProfileStorage;
10
use SixtyEightPublishers\Application\ProfileConfigurationException;
11
12
class SessionProfileStorage implements IProfileStorage
13
{
14
	use SmartObject;
15
16
	const SESSION_SECTION = 'SixtyEightPublishers.Application';
17
18
	/** @var null|ActiveProfile */
19
	private $profile;
20
21
	/** @var \Nette\Http\SessionSection  */
22
	private $session;
23
24
	/** @var null|callable */
25
	public $onPersist;
26
27
	/** @var null|callable */
28
	public $onProfileSet;
29
30
	/**
31
	 * @param \Nette\Http\Session $session
32
	 */
33
	public function __construct(Session $session)
34
	{
35
		$this->session = $session->getSection(self::SESSION_SECTION);
36
	}
37
38
39
	/***************** interface \SixtyEightPublishers\Application\IProfileStorage *****************/
40
41
	/**
42
	 * {@inheritdoc}
43
	 */
44
	public function setProfile(IProfile $profile)
45
	{
46
		$this->profile = new ActiveProfile($profile, $this);
47
48
		if ($this->session['profileName'] === $this->profile->getName()) {
49
			try {
50
				$this->profile->changeCountry($this->session['profileCountry'], FALSE);
51
			} catch (ProfileConfigurationException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
52
			}
53
			try {
54
				$this->profile->changeLanguage($this->session['profileLanguage'], FALSE);
55
			} catch (ProfileConfigurationException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
56
			}
57
			try {
58
				$this->profile->changeCurrency($this->session['profileCurrency'], FALSE);
59
			} catch (ProfileConfigurationException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
60
			}
61
		} else {
62
			$this->session['profileName'] = $this->profile->getName();
63
		}
64
65
		$this->onProfileSet($this->profile);
0 ignored issues
show
Documentation Bug introduced by
The method onProfileSet does not exist on object<SixtyEightPublish...\SessionProfileStorage>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
66
	}
67
68
	/**
69
	 * @return \SixtyEightPublishers\Application\ActiveProfile
70
	 */
71
	public function getProfile() : ActiveProfile
72
	{
73
		return $this->profile;
74
	}
75
76
	/**
77
	 * {@inheritdoc}
78
	 */
79
	public function persist()
80
	{
81
		$this->onPersist($this->profile);
0 ignored issues
show
Bug introduced by
The method onPersist() does not exist on SixtyEightPublishers\App...e\SessionProfileStorage. Did you maybe mean persist()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
82
		$this->session['profileName'] = $this->profile->getName();
83
		$this->session['profileCountry'] = $this->profile->getCountry();
84
		$this->session['profileLanguage'] = $this->profile->getLanguage();
85
		$this->session['profileCurrency'] = $this->profile->getCurrency();
86
	}
87
}
88