diff --git a/Mite/Mite.php b/Mite/Mite.php index 293fd5d..9494bed 100644 --- a/Mite/Mite.php +++ b/Mite/Mite.php @@ -296,17 +296,17 @@ public function addService(Array $data) * Start tracking * * @param int $id + * @return MiteTracker */ public function startTracking($id) { //PATCH /tracker/:id.json try { - - $response = $this->Resty->patch('/tracker/'.$id.'.json'); + $response = $this->Resty->patch('tracker/'.$id.'.json'); if ($this->Resty->getInfo('http_code') == '200') { - return $response; + return MiteTracker::fromResponse($response); } } catch(\Exception $e) @@ -316,6 +316,53 @@ public function startTracking($id) return false; } + + /** + * get tracking information as either MiteTracker entity if tracker is running, or null if no time entry is currently tracked + * @return MiteTracker|null + */ + public function getTracking() + { + //GET /tracker.json + try + { + $response = $this->Resty->get('tracker.json'); + + if ($this->Resty->getInfo('http_code') == '200') { + return MiteTracker::fromResponse($response); + } + } + catch(\Exception $e) + { + throw new MiteException('Tracking couldn\'t not be fetched ('.$e->getMessage().').'); + } + + return null; + } + + /** + * Delete tracking and returns the deleted MiteTracker entity + * + * @param int $id + * @return MiteTracker|null + */ + public function stopTracking($id) + { + try + { + $response = $this->Resty->delete('tracker/'.$id.'.json'); + if ($this->Resty->getInfo('http_code') == '200') + { + return MiteTracker::fromResponse($response); + } + } + catch (\Exception $e) + { + throw new MiteException('Entity deletion failed "tracker" ('.$e->getMessage().').'); + } + + return null; + } /** * Add entity diff --git a/Mite/MiteTracker.php b/Mite/MiteTracker.php new file mode 100644 index 0000000..f2c6447 --- /dev/null +++ b/Mite/MiteTracker.php @@ -0,0 +1,44 @@ + + */ +class MiteTracker extends MiteEntity +{ + /** + * ID to the current active time entry (MiteTime) + * @var integer + */ + public $id = null; + + /** + * The duration of the current time entry in minutes + * @var integer + */ + public $minutes = null; + + /** + * The start time of the current time entry in DATE_RFC3339 format + * @var string + */ + public $since = null; + + /** + * factory for MiteTracker entities, out of a response object + * @param \stdClass $response + * @return MiteTracker|null + */ + public static function fromResponse(\stdClass $response) { + if(!empty($response->tracker->stopped_time_entry)) { + return new self($response->tracker->stopped_time_entry); + } + if(!empty($response->tracker->tracking_time_entry)) { + return new self($response->tracker->tracking_time_entry); + } + return null; + } +} diff --git a/README.md b/README.md index 7f3de05..14401f3 100644 --- a/README.md +++ b/README.md @@ -77,6 +77,20 @@ $newtime = $mite->addTime( ); ``` +Start and stop the current running time: +```php +getTracking(); + +// stop the tracker, where $tracker->id is the id of the corresponding time entry +$mite->stopTracking($tracker->id); + +// and start it again: +$mite->startTracking($tracker->id); + +``` + Look into the code `Mite/Mite.php` for a complete list of methods this library provides. Contact