1: <?php
2: /**
3: * Magento
4: *
5: * NOTICE OF LICENSE
6: *
7: * This source file is subject to the Open Software License (OSL 3.0)
8: * that is bundled with this package in the file LICENSE.txt.
9: * It is also available through the world-wide-web at this URL:
10: * http://opensource.org/licenses/osl-3.0.php
11: * If you did not receive a copy of the license and are unable to
12: * obtain it through the world-wide-web, please send an email
13: * to license@magentocommerce.com so we can send you a copy immediately.
14: *
15: * DISCLAIMER
16: *
17: * Do not edit or add to this file if you wish to upgrade Magento to newer
18: * versions in the future. If you wish to customize Magento for your
19: * needs please refer to http://www.magentocommerce.com for more information.
20: *
21: * @category Mage
22: * @package Mage_Api2
23: * @copyright Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
24: * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
25: */
26:
27: /**
28: * Webservice api2 dispatcher model
29: *
30: * @category Mage
31: * @package Mage_Api2
32: * @author Magento Core Team <core@magentocommerce.com>
33: */
34: class Mage_Api2_Model_Dispatcher
35: {
36: /**
37: * Template for retrieve resource class name
38: */
39: const RESOURCE_CLASS_TEMPLATE = ':resource_:api_:user_v:version';
40:
41: /**
42: * API User object
43: *
44: * @var Mage_Api2_Model_Auth_User_Abstract
45: */
46: protected $_apiUser;
47:
48: /**
49: * Instantiate resource class, set parameters to the instance, run resource internal dispatch method
50: *
51: * @param Mage_Api2_Model_Request $request
52: * @param Mage_Api2_Model_Response $response
53: * @return Mage_Api2_Model_Dispatcher
54: * @throws Mage_Api2_Exception
55: */
56: public function dispatch(Mage_Api2_Model_Request $request, Mage_Api2_Model_Response $response)
57: {
58: if (!$request->getModel() || !$request->getApiType()) {
59: throw new Mage_Api2_Exception(
60: 'Request does not contains all necessary data', Mage_Api2_Model_Server::HTTP_BAD_REQUEST
61: );
62: }
63: $model = self::loadResourceModel(
64: $request->getModel(),
65: $request->getApiType(),
66: $this->getApiUser()->getType(),
67: $this->getVersion($request->getResourceType(), $request->getVersion())
68: );
69:
70: $model->setRequest($request);
71: $model->setResponse($response);
72: $model->setApiUser($this->getApiUser());
73:
74: $model->dispatch();
75:
76: return $this;
77: }
78:
79: /**
80: * Pack resource model class path from components and try to load it
81: *
82: * @param string $apiType API type
83: * @param string $userType API User type (e.g. admin, customer, guest)
84: * @param int $version Requested version
85: * @return Mage_Api2_Model_Resource
86: * @throws Mage_Api2_Exception
87: */
88: public static function loadResourceModel($model, $apiType, $userType, $version)
89: {
90: $class = strtr(
91: self::RESOURCE_CLASS_TEMPLATE,
92: array(':resource' => $model, ':api' => $apiType, ':user' => $userType, ':version' => $version)
93: );
94:
95: try {
96: /** @var $modelObj Mage_Api2_Model_Resource */
97: $modelObj = Mage::getModel($class);
98: } catch (Exception $e) {
99: // getModel() throws exception when in application is in development mode - skip it to next check
100: }
101: if (empty($modelObj) || !$modelObj instanceof Mage_Api2_Model_Resource) {
102: throw new Mage_Api2_Exception('Resource not found', Mage_Api2_Model_Server::HTTP_NOT_FOUND);
103: }
104: return $modelObj;
105: }
106:
107: /**
108: * Set API user object
109: *
110: * @param Mage_Api2_Model_Auth_User_Abstract $apiUser
111: * @return Mage_Api2_Model_Dispatcher
112: */
113: public function setApiUser(Mage_Api2_Model_Auth_User_Abstract $apiUser)
114: {
115: $this->_apiUser = $apiUser;
116:
117: return $this;
118: }
119:
120: /**
121: * Get API user object
122: *
123: * @return Mage_Api2_Model_Auth_User_Abstract
124: */
125: public function getApiUser()
126: {
127: if (!$this->_apiUser) {
128: throw new Exception('API user is not set.');
129: }
130:
131: return $this->_apiUser;
132: }
133:
134: /**
135: * Get correct version of the resource model
136: *
137: * @param string $resourceType
138: * @param string|bool $requestedVersion
139: * @return int
140: * @throws Mage_Api2_Exception
141: */
142: public function getVersion($resourceType, $requestedVersion)
143: {
144: if (false !== $requestedVersion && !preg_match('/^[1-9]\d*$/', $requestedVersion)) {
145: throw new Mage_Api2_Exception(
146: sprintf('Invalid version "%s" requested.', htmlspecialchars($requestedVersion)),
147: Mage_Api2_Model_Server::HTTP_BAD_REQUEST
148: );
149: }
150: return $this->getConfig()->getResourceLastVersion($resourceType, $requestedVersion);
151: }
152:
153: /**
154: * Get config
155: *
156: * @return Mage_Api2_Model_Config
157: */
158: public function getConfig()
159: {
160: return Mage::getModel('api2/config');
161: }
162: }
163: