Upgrade phpCAS
[piwik-CASLogin.git] / CAS / CAS / ProxiedService / Abstract.php
1 <?php
2
3 /**
4  * Licensed to Jasig under one or more contributor license
5  * agreements. See the NOTICE file distributed with this work for
6  * additional information regarding copyright ownership.
7  *
8  * Jasig licenses this file to you under the Apache License,
9  * Version 2.0 (the "License"); you may not use this file except in
10  * compliance with the License. You may obtain a copy of the License at:
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  * PHP Version 5
21  *
22  * @file     CAS/ProxiedService/Abstract.php
23  * @category Authentication
24  * @package  PhpCAS
25  * @author   Adam Franco <afranco@middlebury.edu>
26  * @license  http://www.apache.org/licenses/LICENSE-2.0  Apache License 2.0
27  * @link     https://wiki.jasig.org/display/CASC/phpCAS
28  */
29
30 /**
31  * This class implements common methods for ProxiedService implementations included
32  * with phpCAS.
33  *
34  * @class    CAS_ProxiedService_Abstract
35  * @category Authentication
36  * @package  PhpCAS
37  * @author   Adam Franco <afranco@middlebury.edu>
38  * @license  http://www.apache.org/licenses/LICENSE-2.0  Apache License 2.0
39  * @link     https://wiki.jasig.org/display/CASC/phpCAS
40  */
41 abstract class CAS_ProxiedService_Abstract
42 implements CAS_ProxiedService, CAS_ProxiedService_Testable
43 {
44
45     /**
46      * The proxy ticket that can be used when making service requests.
47      * @var string $_proxyTicket;
48      */
49     private $_proxyTicket;
50
51     /**
52      * Register a proxy ticket with the Proxy that it can use when making requests.
53      *
54      * @param string $proxyTicket proxy ticket
55      *
56      * @return void
57      * @throws InvalidArgumentException If the $proxyTicket is invalid.
58      * @throws CAS_OutOfSequenceException If called after a proxy ticket has already been initialized/set.
59      */
60     public function setProxyTicket ($proxyTicket)
61     {
62         if (empty($proxyTicket)) {
63             throw new CAS_InvalidArgumentException("Trying to initialize with an empty proxy ticket.");
64         }
65         if (!empty($this->_proxyTicket)) {
66             throw new CAS_OutOfSequenceException('Already initialized, cannot change the proxy ticket.');
67         }
68         $this->_proxyTicket = $proxyTicket;
69     }
70
71     /**
72      * Answer the proxy ticket to be used when making requests.
73      *
74      * @return string
75      * @throws CAS_OutOfSequenceException If called before a proxy ticket has
76      * already been initialized/set.
77      */
78     protected function getProxyTicket ()
79     {
80         if (empty($this->_proxyTicket)) {
81             throw new CAS_OutOfSequenceException('No proxy ticket yet. Call $this->initializeProxyTicket() to aquire the proxy ticket.');
82         }
83
84         return $this->_proxyTicket;
85     }
86
87     /**
88      * @var CAS_Client $_casClient;
89      */
90     private $_casClient;
91
92     /**
93      * Use a particular CAS_Client->initializeProxiedService() rather than the
94      * static phpCAS::initializeProxiedService().
95      *
96      * This method should not be called in standard operation, but is needed for unit
97      * testing.
98      *
99      * @param CAS_Client $casClient cas client
100      *
101      * @return void
102      * @throws CAS_OutOfSequenceException If called after a proxy ticket has
103      * already been initialized/set.
104      */
105     public function setCasClient (CAS_Client $casClient)
106     {
107         if (!empty($this->_proxyTicket)) {
108             throw new CAS_OutOfSequenceException('Already initialized, cannot change the CAS_Client.');
109         }
110
111         $this->_casClient = $casClient;
112     }
113
114     /**
115      * Fetch our proxy ticket.
116      *
117      * Descendent classes should call this method once their service URL is available
118      * to initialize their proxy ticket.
119      *
120      * @return void
121      * @throws CAS_OutOfSequenceException If called after a proxy ticket has
122      * already been initialized.
123      */
124     protected function initializeProxyTicket()
125     {
126         if (!empty($this->_proxyTicket)) {
127             throw new CAS_OutOfSequenceException('Already initialized, cannot initialize again.');
128         }
129         // Allow usage of a particular CAS_Client for unit testing.
130         if (empty($this->_casClient)) {
131             phpCAS::initializeProxiedService($this);
132         } else {
133             $this->_casClient->initializeProxiedService($this);
134         }
135     }
136
137 }
138 ?>