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_Customer
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: * Abstract API2 class for customer
29: *
30: * @category Mage
31: * @package Mage_Customer
32: * @author Magento Core Team <core@magentocommerce.com>
33: */
34: abstract class Mage_Customer_Model_Api2_Customer_Rest extends Mage_Customer_Model_Api2_Customer
35: {
36: /**
37: * Create customer
38: *
39: * @param array $data
40: * @return string
41: */
42: protected function _create(array $data)
43: {
44: /** @var $validator Mage_Api2_Model_Resource_Validator_Eav */
45: $validator = Mage::getResourceModel('api2/validator_eav', array('resource' => $this));
46:
47: $data = $validator->filter($data);
48: if (!$validator->isValidData($data)) {
49: foreach ($validator->getErrors() as $error) {
50: $this->_error($error, Mage_Api2_Model_Server::HTTP_BAD_REQUEST);
51: }
52: $this->_critical(self::RESOURCE_DATA_PRE_VALIDATION_ERROR);
53: }
54:
55: /** @var $customer Mage_Customer_Model_Customer */
56: $customer = Mage::getModel('customer/customer');
57: $customer->setData($data);
58:
59: try {
60: $customer->save();
61: } catch (Mage_Core_Exception $e) {
62: $this->_error($e->getMessage(), Mage_Api2_Model_Server::HTTP_INTERNAL_ERROR);
63: } catch (Exception $e) {
64: $this->_critical(self::RESOURCE_INTERNAL_ERROR);
65: }
66:
67: return $this->_getLocation($customer);
68: }
69:
70: /**
71: * Retrieve information about customer
72: *
73: * @throws Mage_Api2_Exception
74: * @return array
75: */
76: protected function _retrieve()
77: {
78: /** @var $customer Mage_Customer_Model_Customer */
79: $customer = $this->_loadCustomerById($this->getRequest()->getParam('id'));
80: return $customer->getData();
81: }
82:
83: /**
84: * Get customers list
85: *
86: * @return array
87: */
88: protected function _retrieveCollection()
89: {
90: $data = $this->_getCollectionForRetrieve()->load()->toArray();
91: return isset($data['items']) ? $data['items'] : $data;
92: }
93:
94: /**
95: * Update customer
96: *
97: * @param array $data
98: * @throws Mage_Api2_Exception
99: */
100: protected function _update(array $data)
101: {
102: /** @var $customer Mage_Customer_Model_Customer */
103: $customer = $this->_loadCustomerById($this->getRequest()->getParam('id'));
104: /** @var $validator Mage_Api2_Model_Resource_Validator_Eav */
105: $validator = Mage::getResourceModel('api2/validator_eav', array('resource' => $this));
106:
107: $data = $validator->filter($data);
108:
109: unset($data['website_id']); // website is not allowed to change
110:
111: if (!$validator->isValidData($data, true)) {
112: foreach ($validator->getErrors() as $error) {
113: $this->_error($error, Mage_Api2_Model_Server::HTTP_BAD_REQUEST);
114: }
115: $this->_critical(self::RESOURCE_DATA_PRE_VALIDATION_ERROR);
116: }
117:
118: $customer->addData($data);
119:
120: try {
121: $customer->save();
122: } catch (Mage_Core_Exception $e) {
123: $this->_error($e->getMessage(), Mage_Api2_Model_Server::HTTP_INTERNAL_ERROR);
124: } catch (Exception $e) {
125: $this->_critical(self::RESOURCE_INTERNAL_ERROR);
126: }
127: }
128:
129: /**
130: * Load customer by id
131: *
132: * @param int $id
133: * @throws Mage_Api2_Exception
134: * @return Mage_Customer_Model_Customer
135: */
136: protected function _loadCustomerById($id)
137: {
138: /** @var $customer Mage_Customer_Model_Customer */
139: $customer = Mage::getModel('customer/customer')->load($id);
140: if (!$customer->getId()) {
141: $this->_critical(self::RESOURCE_NOT_FOUND);
142: }
143: return $customer;
144: }
145:
146: /**
147: * Retrieve collection instances
148: *
149: * @return Mage_Customer_Model_Resource_Customer_Collection
150: */
151: protected function _getCollectionForRetrieve()
152: {
153: /** @var $collection Mage_Customer_Model_Resource_Customer_Collection */
154: $collection = Mage::getResourceModel('customer/customer_collection');
155: $collection->addAttributeToSelect(array_keys(
156: $this->getAvailableAttributes($this->getUserType(), Mage_Api2_Model_Resource::OPERATION_ATTRIBUTE_READ)
157: ));
158:
159: $this->_applyCollectionModifiers($collection);
160: return $collection;
161: }
162: }
163: