1: <?php
2: /**
3: * Update the freecode information.
4: *
5: * PHP version 5
6: *
7: * @category Horde
8: * @package Release
9: * @author Mike Hardy
10: * @author Jan Schneider <jan@horde.org>
11: * @author Gunnar Wrobel <wrobel@pardus.de>
12: * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
13: * @link http://www.horde.org/libraries/Horde_Release
14: */
15:
16: /**
17: * Update the freecode information.
18: *
19: * Copyright 2011-2012 Horde LLC (http://www.horde.org/)
20: *
21: * See the enclosed file COPYING for license information (LGPL). If you
22: * did not receive this file, see http://www.horde.org/licenses/lgpl21.
23: *
24: * @category Horde
25: * @package Release
26: * @author Mike Hardy
27: * @author Jan Schneider <jan@horde.org>
28: * @author Gunnar Wrobel <wrobel@pardus.de>
29: * @license http://www.horde.org/licenses/lgpl21 LGPL 2.1
30: * @link http://www.horde.org/libraries/Horde_Release
31: */
32: class Horde_Release_Freecode
33: {
34: /**
35: * Freecode auth token.
36: *
37: * @var string
38: */
39: private $_token;
40:
41: /**
42: * Freecode project name.
43: *
44: * @var string
45: */
46: private $_project;
47:
48: /**
49: * Constructor.
50: *
51: * @param string $token Freecode auth token.
52: * @param string $proect Freecode project name.
53: */
54: public function __construct($token, $project)
55: {
56: $this->_token = $token;
57: $this->_project = $project;
58: }
59:
60: /**
61: * Attempt to publish the new release to the fm restful api.
62: *
63: * @param array $params The array of fm release parameters
64: *
65: * @return mixed Result of the attempt / PEAR_Error on failure
66: */
67: public function publish($params)
68: {
69: $params['tag_list'] = implode(', ', $params['tag_list']);
70: $fm_params = array('auth_code' => $this->_token,
71: 'release' => $params);
72: $http = new Horde_Http_Client();
73: try {
74: $response = $http->post('http://freecode.com/projects/' . $this->_project . '/releases.json',
75: Horde_Serialize::serialize($fm_params, Horde_Serialize::JSON),
76: array('Content-Type' => 'application/json'));
77: } catch (Horde_Http_Exception $e) {
78: if (strpos($e->getMessage(), '201 Created') === false) {
79: throw new Horde_Exception_Wrapped($e);
80: } else {
81: return '';
82: }
83: }
84:
85: // 201 Created
86: return $response->getBody();
87: }
88:
89: /**
90: * Attempt to update FM project links
91: */
92: public function updateLinks($links)
93: {
94: // Need to get the list of current URLs first, then find the one we want
95: // to update.
96: $http = new Horde_Http_Client();
97: try {
98: $response = $http->get('http://freecode.com/projects/' . $this->_project . '/urls.json?auth_code=' . $this->_token);
99: } catch (Horde_Http_Exception $e) {
100: throw new Horde_Exception_Wrapped($e);
101: }
102:
103: $url_response = Horde_Serialize::unserialize($response->getBody(), Horde_Serialize::JSON);
104: if (!is_array($url_response)) {
105: $url_response = array();
106: }
107:
108: // Should be an array of URL info in response...go through our requested
109: // updates and see if we can find the correct 'permalink' parameter.
110: foreach ($links as $link) {
111: $permalink = '';
112: foreach ($url_response as $url) {
113: // FM docs contradict this, but each url entry in the array is
114: // wrapped in a 'url' property.
115: $url = $url->url;
116: if ($link['label'] == $url->label) {
117: $permalink = $url->permalink;
118: break;
119: }
120: }
121: $link = array('auth_code' => $this->_token,
122: 'url' => $link);
123: $http = new Horde_Http_Client();
124: if (empty($permalink)) {
125: // No link found to update...create it.
126: try {
127: $response = $http->post('http://freecode.com/projects/' . $this->_project . '/urls.json',
128: Horde_Serialize::serialize($link, Horde_Serialize::JSON),
129: array('Content-Type' => 'application/json'));
130: $response = $response->getBody();
131: } catch (Horde_Http_Exception $e) {
132: if (strpos($e->getMessage(), '201 Created') === false) {
133: throw new Horde_Exception_Wrapped($e);
134: } else {
135: $response = '';
136: }
137: }
138: } else {
139: // Found the link to update...update it.
140: try {
141: $response = $http->put('http://freecode.com/projects/' . $this->_project . '/urls/' . $permalink . '.json',
142: Horde_Serialize::serialize($link, Horde_Serialize::JSON),
143: array('Content-Type' => 'application/json'));
144: $response = $response->getBody();
145: // Status: 200???
146: } catch (Horde_Http_Exception $e) {
147: throw new Horde_Exception_Wrapped($e);
148: }
149: }
150: }
151:
152: return true;
153: }
154: }
155: