Completed
Push — master ( ce34f1...a61f8e )
by
unknown
04:41 queued 10s
created

SessionDownloadStrategy::getSession()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the Sonata Project package.
5
 *
6
 * (c) Thomas Rabaix <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sonata\MediaBundle\Security;
13
14
use Sonata\MediaBundle\Model\MediaInterface;
15
use Symfony\Component\DependencyInjection\ContainerInterface;
16
use Symfony\Component\HttpFoundation\Request;
17
use Symfony\Component\HttpFoundation\Session\SessionInterface;
18
use Symfony\Component\Translation\TranslatorInterface;
19
20
/**
21
 * Class SessionDownloadStrategy.
22
 *
23
 * @author Ahmet Akbana <[email protected]>
24
 */
25
class SessionDownloadStrategy implements DownloadStrategyInterface
26
{
27
    /**
28
     * @var ContainerInterface
29
     *
30
     * @deprecated Since version 3.x, will be removed in 4.0.
31
     * NEXT_MAJOR : remove this property
32
     */
33
    protected $container;
34
35
    /**
36
     * @var TranslatorInterface
37
     */
38
    protected $translator;
39
40
    /**
41
     * @var int
42
     */
43
    protected $times;
44
45
    /**
46
     * @var string
47
     */
48
    protected $sessionKey = 'sonata/media/session/times';
49
50
    /**
51
     * @var SessionInterface
52
     */
53
    private $session;
54
55
    /**
56
     * @param TranslatorInterface                 $translator
57
     * @param ContainerInterface|SessionInterface $session
58
     * @param int                                 $times
59
     */
60
    public function __construct(TranslatorInterface $translator, $session, $times)
61
    {
62
        // NEXT_MAJOR : remove this block and set session from parameter.
63
        if ($session instanceof ContainerInterface) {
64
            @trigger_error(
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
65
                'Using an instance of Symfony\Component\DependencyInjection\ContainerInterface is deprecated since 
66
                version 3.x and will be removed in 4.0. 
67
                Use Symfony\Component\HttpFoundation\Session\SessionInterface instead.',
68
                E_USER_DEPRECATED
69
            );
70
71
            $this->session = $session->get('session');
72
        } elseif ($session instanceof SessionInterface) {
73
            $this->session = $session;
74
        } else {
75
            throw new \InvalidArgumentException(
76
                '$session should be an instance of Symfony\Component\HttpFoundation\Session\SessionInterface'
77
            );
78
        }
79
80
        $this->times = $times;
81
        $this->translator = $translator;
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87
    public function isGranted(MediaInterface $media, Request $request)
88
    {
89
        $times = $this->session->get($this->sessionKey, 0);
90
91
        if ($times >= $this->times) {
92
            return false;
93
        }
94
95
        ++$times;
96
97
        $this->session->set($this->sessionKey, $times);
98
99
        return true;
100
    }
101
102
    /**
103
     * {@inheritdoc}
104
     */
105
    public function getDescription()
106
    {
107
        return $this->translator->transChoice(
108
            'description.session_download_strategy',
109
            $this->times,
110
            array('%times%' => $this->times),
111
            'SonataMediaBundle'
112
        );
113
    }
114
}
115