Skip to content

Commit 149c068

Browse files
author
Chris Chambers
committed
Clean up errors with Make Command, extend Task parameters from template, allow override of paramaters from createTask
1 parent f2ab6d7 commit 149c068

File tree

4 files changed

+157
-13
lines changed

4 files changed

+157
-13
lines changed

src/Asana.php

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,12 @@ public function getUsers($opt_fields = null)
105105
*/
106106
public function createTask($data = [])
107107
{
108-
if (isset($this->templateData)) {
109-
$data = $this->templateData;
110-
} elseif (!count($data)) {
111-
throw new \Exception('Asana create task data not set');
108+
if (isset($this->templateData) && isset($data)) {
109+
$data = array_merge($this->templateData, $data);
110+
}
111+
112+
if (!count($data)) {
113+
throw new Exception('Asana create task data not set');
112114
}
113115

114116
$data = array_merge([
@@ -120,13 +122,20 @@ public function createTask($data = [])
120122
}
121123

122124
/**
125+
* Replace data in Task Templates with array variables passed here
126+
*
127+
* USAGE - asana()->getTemplate('App\Asana\CreateSomeAsanaTask')->applyData(['name' => 'My Name'])->createTask();
128+
*
123129
* @param $data
130+
*
124131
* @return $this
132+
* @author Chris Chambers https://github.com/labelworx
133+
* @version 1.0
125134
*/
126135
public function applyData($data)
127136
{
128137
foreach ($data as $key => $value) {
129-
$data['{' . $key . '}'] = $value;
138+
$data['{{' . $key . '}}'] = $value;
130139
unset($data[$key]);
131140
}
132141

@@ -138,11 +147,15 @@ public function applyData($data)
138147
}
139148

140149
/**
141-
* Gets task data from a class
150+
* Gets task data from a defined class. Use php artisan make:asana to stub out a template class
142151
*
143-
* @param $className
152+
* USAGE - asana()->getTemplate('App\Asana\CreateSomeAsanaTask')->createTask();
153+
*
154+
* @param string $className
144155
* @return $this
145156
* @throws Exception
157+
* @author Chris Chambers https://github.com/labelworx
158+
* @version 1.0
146159
*/
147160
public function getTemplate($className)
148161
{

src/AsanaMakeCommand.php

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class AsanaMakeCommand extends Command
1111
*
1212
* @var string
1313
*/
14-
protected $signature = 'make:asana {className : Class (singular) for example ToDoAsanaTask}';
14+
protected $signature = 'make:asana {name : Class (singular) for example ToDoAsanaTask}';
1515

1616
/**
1717
* The console command description.
@@ -34,11 +34,10 @@ public function __construct()
3434
* Execute the console command.
3535
*
3636
* @return bool|void|null
37-
* @throws Exception
3837
*/
3938
public function handle()
4039
{
41-
$this->createTaskClassFile($this->argument('className'));
40+
$this->createTaskClassFile($this->argument('name'));
4241
}
4342

4443
/**
@@ -55,12 +54,14 @@ protected function getStub()
5554
* Create the Asana Task Template Class file
5655
*
5756
* @param $className
58-
* @throws Exception
5957
*/
6058
protected function createTaskClassFile($className)
6159
{
62-
if (file_exists("Asana/{$className}.php")) {
63-
throw new Exception('Asana task class already exists');
60+
$this->checkDirectory();
61+
62+
if (file_exists(app_path('Asana')."/{$className}.php")) {
63+
$this->error('Asana task class already exists');
64+
exit;
6465
}
6566

6667
$taskTemplate = str_replace(
@@ -71,4 +72,17 @@ protected function createTaskClassFile($className)
7172

7273
file_put_contents(app_path("Asana/{$className}.php"), $taskTemplate);
7374
}
75+
76+
/**
77+
* Create the directory for template classes if it does not exist
78+
*/
79+
protected function checkDirectory()
80+
{
81+
if (!file_exists(app_path('Asana'))) {
82+
if (!mkdir(app_path('Asana'))) {
83+
$this->error('Could not create ' . app_path('Asana') . ' directory');
84+
exit;
85+
}
86+
}
87+
}
7488
}

src/AsanaTask.php

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?php
2+
3+
4+
namespace Torann\LaravelAsana;
5+
6+
use Illuminate\Support\Str;
7+
8+
class AsanaTask
9+
{
10+
11+
protected $assignee;
12+
protected $custom_fields = [];
13+
protected $due_at;
14+
protected $due_on;
15+
protected $followers = [];
16+
protected $name;
17+
protected $notes;
18+
protected $parent;
19+
protected $projects = [];
20+
protected $tags = [];
21+
protected $workspace;
22+
23+
private $data = [];
24+
25+
public function buildTaskData()
26+
{
27+
$this->setAssignee();
28+
$this->setCustomFields();
29+
$this->setDueAt();
30+
$this->setDueOn();
31+
$this->setFollowers();
32+
$this->setName();
33+
$this->setNotes();
34+
$this->setParent();
35+
$this->setProjects();
36+
$this->setTags();
37+
$this->setWorkspace();
38+
39+
return $this->data;
40+
}
41+
42+
private function setAssignee() {
43+
if (Str::length($this->assignee)) {
44+
$this->data['assignee'] = $this->assignee;
45+
}
46+
}
47+
48+
private function setCustomFields() {
49+
if (is_array($this->custom_fields) && count($this->custom_fields)) {
50+
$this->data['custom_fields'] = $this->custom_fields;
51+
}
52+
}
53+
54+
private function setDueAt() {
55+
if (Str::length($this->due_at)) {
56+
$this->data['due_at'] = $this->due_at;
57+
}
58+
}
59+
60+
private function setDueOn() {
61+
if (Str::length($this->due_on)) {
62+
$this->data['due_on'] = $this->due_on;
63+
}
64+
}
65+
66+
private function setFollowers() {
67+
if (is_array($this->followers) && count($this->followers)) {
68+
$this->data['followers'] = $this->followers;
69+
}
70+
}
71+
72+
private function setName() {
73+
if (Str::length($this->name)) {
74+
$this->data['name'] = $this->name;
75+
}
76+
}
77+
78+
private function setNotes() {
79+
if (Str::length($this->notes)) {
80+
$this->data['notes'] = $this->notes;
81+
}
82+
}
83+
84+
private function setParent() {
85+
if (Str::length($this->parent)) {
86+
$this->data['parent'] = $this->parent;
87+
}
88+
}
89+
90+
private function setProjects() {
91+
if (is_array($this->projects) && count($this->projects)) {
92+
$this->data['projects'] = $this->projects;
93+
}
94+
}
95+
96+
private function setTags() {
97+
if (is_array($this->tags) && count($this->tags)) {
98+
$this->data['tags'] = $this->tags;
99+
}
100+
}
101+
102+
private function setWorkspace()
103+
{
104+
if (Str::length($this->workspace)) {
105+
$this->data['workspace'] = $this->workspace;
106+
}
107+
}
108+
109+
}

stubs/AsanaTask.stub

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ use \Torann\LaravelAsana\AsanaTask;
66

77
class {{className}} extends AsanaTask
88
{
9+
/**
10+
* You can set parameters in all fields using {{name}} syntax. These can then be swapped out by
11+
* using the applyData() method before you call createTask();
12+
*
13+
* USAGE - asana()->getTemplate('App\Asana\CreateSomeAsanaTask')->applyData(['name' => 'My Name'])->createTask();
14+
*
15+
* NOTE - If you pass data directly into the createTask() method then this will override data set in this class.
16+
*/
917

1018
/**
1119
* The ID or email of the user the task should be assigned to. Leave unset for not assigned.

0 commit comments

Comments
 (0)