Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Working with teamwork writer #27

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions cyb-app/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ QUEUE_CONNECTION=sync
SESSION_DRIVER=file
SESSION_LIFETIME=120

TEAMWORK_DOMAIN=https://pondersource.teamwork.com/
TEAMWORK_SECRET=twp_o36bDN1kMF8gX9Df9vexWB4Dm32W

MEMCACHED_HOST=127.0.0.1

REDIS_HOST=127.0.0.1
Expand Down
55 changes: 55 additions & 0 deletions cyb-app/app/Applications/Teamwork/Auth.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace App\Applications\Teamwork;

class Auth
{
private static $url = 'https://authenticate.teamwork.com/';

private static $config = [
'url' => null,
'key' => null
];

private static $is_subdomain = false;

public static function set()
{
$num_args = func_num_args();
if ($num_args === 1) {
self::$config['url'] = self::$url;
self::$config['key'] = func_get_arg(0);
self::$config['url'] = Factory::build('account')->authenticate()->url;
} elseif ($num_args === 2) {
self::$config['url'] = $url = func_get_arg(0);
self::checkSubDomain($url);
if (self::$is_subdomain) {
self::$config['url'] = self::$url;
}
self::$config['key'] = func_get_arg(1);
if (self::$is_subdomain) {
$test = Factory::build('account')->authenticate();
$url = $test->url;
}
self::$config['url'] = $url;
}
}

public static function get()
{
return array_values(self::$config);
}

private static function checkSubDomain($url)
{
$eu_domain = strpos($url, '.eu');

if ($eu_domain !== false) {
self::$url = 'https://authenticate.eu.teamwork.com/';
$url = substr($url, 0, $eu_domain);
}
if (strpos($url, '.') === false) {
self::$is_subdomain = true;
}
}
}
36 changes: 36 additions & 0 deletions cyb-app/app/Applications/Teamwork/Exception.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace App\Applications\Teamwork;

class Exception extends \ErrorException
{
private $response = null;
private $headers = [];


public function __construct($errorInfo)
{
if (!is_array($errorInfo)) {
$message = $errorInfo;
$errorInfo = [];
$errorInfo['Message'] = $message;
}
$this->message = trim($errorInfo['Message']);
if (isset($errorInfo['Response'])) {
$this->response = $errorInfo['Response'];
}
if (isset($errorInfo['Headers'])) {
$this->headers = $errorInfo['Headers'];
}
}

public function getResponse()
{
return $this->response;
}

public function getHeaders()
{
return $this->headers;
}
}
21 changes: 21 additions & 0 deletions cyb-app/app/Applications/Teamwork/Factory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace App\Applications\Teamwork;

class Factory
{
public static function build($class_name)
{
$class_name = str_replace(['/', '.'], '\\', $class_name);
$class_name = preg_replace_callback('/(\\\.)/', function ($matches) {
return strtoupper($matches[1]);
}, $class_name);
$class_name = ucfirst($class_name);
if (strcasecmp($class_name, 'task\\list') === 0) {
$class_name = 'Task_List';
}
$class_name = '\\' . __NAMESPACE__ . '\\' . $class_name;

return forward_static_call_array([$class_name, 'getInstance'], Auth::get());
}
}
43 changes: 43 additions & 0 deletions cyb-app/app/Applications/Teamwork/Helper/Str.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace App\Applications\Teamwork\Helper;

// from https://github.com/laravel/framework/blob/master/src/Illuminate/Support/Str.php

final class Str
{
/**
* Convert a value to camel case.
*
* @param string $value
* @return string
*/
public static function camel($value)
{
return lcfirst(static::studly($value));
}

/**
* Convert a value to studly caps case.
*
* @param string $value
* @return string
*/
public static function studly($value)
{
$value = ucwords(str_replace(['-', '_'], ' ', $value));

return str_replace(' ', '', $value);
}

/**
* Convert all undescores into dashes
*
* @param string $value
* @return string
*/
public static function dash($value)
{
return str_replace('_', '-', $value);
}
}
165 changes: 165 additions & 0 deletions cyb-app/app/Applications/Teamwork/Milestone.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
<?php

namespace App\Applications\Teamwork;

class Milestone extends Model
{
protected function init()
{
// this is the list of fields that can send the api
$this->fields = [
'title' => true,
'description' => false,
'deadline' => [
'required'=>true,
'attributes'=>[
'type'=>'integer'
]
],//format YYYYMMDD
'notify' => [
'required'=>false,
'attributes'=>[
'type'=>'boolean'
]
],
'reminder'=>[
'required'=>false,
'attributes'=>[
'type'=>'boolean'
]
],
'private'=>[
'required'=>false,
'attributes'=>[
'type'=>'boolean'
]
],
'responsible_party_ids' => true,
# USE ONLY FOR UPDATE OR PUT METHOD
'move_upcoming_milestones'=>[
'sibling'=>true,
'required'=>false,
'attributes'=>['type'=>'boolean']
],
'move_upcoming_milestones_off_weekends'=>[
'sibling'=>true,
'required'=>false,
'attributes'=>['type'=>'boolean']
]
];
}

/**
* Complete
*
* PUT /milestones/#{id}/complete.xml
*
* Marks the specified milestone as complete. Returns Status 200 OK.
*
* @param int $id
*
* @return bool
* @throws App\Applications\Teamwork\Exception
*/
public function complete($id)
{
$id = (int) $id;
if ($id <= 0) {
throw new Exception('Invalid param id');
}
return $this->rest->put("$this->action/$id/complete");
}

/**
* Uncomplete
*
* PUT /milestones/#{id}/uncomplete.xml
*
* Marks the specified milestone as uncomplete. Returns Status 200 OK.
*
* @param int $id
*
* @return bool
* @throws \App\Applications\Teamwork\Exception
*/
public function uncomplete($id)
{
$id = (int) $id;
if ($id <= 0) {
throw new Exception('Invalid param id');
}
return $this->rest->put("$this->action/$id/uncomplete");
}

/**
* Get all milestone
*
* @param string $filter
*
* @return @return \App\Applications\Teamwork\Response\Model
* @throws @return \App\Applications\Teamwork\Exception
*/
public function getAll($filter = 'all')
{
return $this->rest->get("$this->action", $this->getParams($filter));
}

/**
* Get all milestone
*
* @param $project_id
* @param string $filter
*
* @return @return \App\Applications\Teamwork\Response\Model
* @throws @return \App\Applications\Teamwork\Exception
*/
public function getByProject($project_id, $filter = 'all')
{
$project_id = (int) $project_id;
if ($project_id <= 0) {
throw new Exception('Invalid param project_id');
}
return $this->rest->get(
"projects/$project_id/$this->action",
$this->getParams($filter)
);
}

/**
* @param $filter
*
* @return array
* @throws \App\Applications\Teamwork\Exception
*/
private function getParams($filter)
{
$params = [];
if ($filter) {
$filter = (string) $filter;
$filter = strtolower($filter);
if ($filter !== 'all') {
$validate = ['completed', 'incomplete', 'late', 'upcoming'];
if (!in_array($filter, $validate)) {
throw new Exception('Invalid value for param filter');
}
$params['find'] = $filter;
}
}
return $params;
}

/**
* @param array $data
*
* @return int
* @throws \App\Applications\Teamwork\Exception
*/
public function insert(array $data)
{
$project_id = empty($data['project_id']) ? 0: $data['project_id'];
if ($project_id <= 0) {
throw new Exception('Required field project_id');
}
return $this->rest->post("projects/$project_id/$this->action", $data);
}
}
Loading