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

Refactor directus model #488

Merged
merged 4 commits into from
Aug 22, 2022
Merged
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
104 changes: 6 additions & 98 deletions app/DirectUs.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,120 +10,28 @@ class DirectUs
{

/**
* @var string
* @param string $_endpoint
* @param array $_args
* @param string|null $_fields
*/
public string $_directUsUrl = 'https://content.fitz.ms/fitz-website/items/';

/**
* @var string
*/
public string $_endpoint = '';
/**
* @var array
*/
public array $_args = array();

/**
* @var string
*/
protected string $_meta = 'meta=total_count,result_count';
/**
* @var string
*/
protected string $_fields = '*.*.*';

/**
* @return string
*/
public function getFields(): string
{
return $this->_fields;
}

/**
* @param string $fields
* @return string
*/
public function setFields(string $fields): string
{
$this->_fields = $fields;
return $this->_fields;
}

/**
* @return string
*/
public function getMeta(): string
{
return $this->_meta;
}

/**
* @param array $args
* @return $this
*/
public function setArguments(array $args): static
public function __construct(public string $_endpoint, public array $_args, public ?string $_fields = null)
{
$this->_args = $args;
return $this;
}

/**
* @return string
*/
#[Pure] public function getCallUrl(): string
{
return $this->getDirectUsUrl() . $this->getEndPoint() . $this->buildQuery();
}

/**
* @return string
*/
public function getDirectUsUrl(): string
{
return $this->_directUsUrl;
}

/**
* @return string
*/
public function getEndPoint(): string
{
return $this->_endpoint;
}

/**
* @param $endpoint
* @return string
*/
public function setEndpoint($endpoint): string
{
$this->_endpoint = $endpoint . '?';
return $this->_endpoint;
}

/**
* @return string
*/
#[Pure] public function buildQuery(): string
{
return http_build_query($this->getArgs());
}

/**
* @return array
*/
public function getArgs(): array
{
return $this->_args;
return '?' . http_build_query($this->_args);
}

/**
* @return array
*/
public function getData(): array
{
$url = $this->getDirectUsUrl() . $this->getEndPoint() . $this->buildQuery();
$url = env('DIRECTUS_URL') . $this->_endpoint . $this->buildQuery();
$key = md5($url);
$expiresAt = now()->addMinutes(20);
if (Cache::has($key)) {
Expand Down
7 changes: 0 additions & 7 deletions app/Http/Controllers/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,6 @@ class Controller extends BaseController
{
use AuthorizesRequests, DispatchesJobs, ValidatesRequests;

/**
* @return DirectUs
*/
#[Pure] public function getApi(): DirectUs
{
return new DirectUs;
}

/**
* @return string
Expand Down
1 change: 1 addition & 0 deletions app/Http/Controllers/collectionsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,5 @@ public static function getObjects(string $prirefs): array
{
return CIIM::findByPriRefs($prirefs);
}

}
29 changes: 4 additions & 25 deletions app/Http/Controllers/researchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use App\Models\ResearchProjects;
use App\Models\StaffProfiles;
use App\Models\ExternalCurators;
use App\Models\Stubs;
use Illuminate\Contracts\View\View;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
Expand All @@ -21,30 +22,8 @@ class researchController extends Controller
*/
public function index(): View
{
$api = $this->getApi();
$api->setEndpoint('stubs_and_pages');
$api->setArguments(
array(
'fields' => '*.*.*.*',
'meta' => 'result_count,total_count,type',
'filter[landing_page][eq]' => '1',
'filter[section][eq]' => 'research',
)
);
$pages = $api->getData();

$apiRes = $this->getApi();
$apiRes->setEndpoint('stubs_and_pages');
$apiRes->setArguments(
array(
'fields' => '*.*.*.*',
'meta' => 'result_count,total_count,type',
'filter[landing_page][null]' => '',
'filter[section][eq]' => 'research',
'filter[associate_with_landing_page][eq]' => '1'
)
);
$associated = $apiRes->getData();
$pages = Stubs::getLandingBySection('research');
$associated = Stubs::getAssociated('research');
return view('research.index', compact('pages', 'associated'));
}

Expand All @@ -70,7 +49,6 @@ public function project(string $slug): View|Response
if (empty($projects['data'])) {
return response()->view('errors.404', [], 404);
}

return view('research.project', compact('projects', 'records'));
}

Expand Down Expand Up @@ -137,6 +115,7 @@ public function resource(string $slug): View|Response
}

/**
* @param Request $request
* @return View
*/
public function resources(Request $request): View
Expand Down
27 changes: 3 additions & 24 deletions app/Http/Controllers/visitController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\Models\FloorPlans;
use App\Models\Transport;
use App\Models\Faqs;
use App\Models\Stubs;
use App\Models\VisitUsComponents;
use Illuminate\Contracts\View\View;

Expand All @@ -17,30 +18,8 @@ class visitController extends Controller
*/
public function index(): View
{
$api = $this->getApi();
$api->setEndpoint('stubs_and_pages');
$api->setArguments(
array(
'fields' => '*.*.*.*',
'meta' => 'result_count,total_count,type',
'filter[section][eq]' => 'visit-us',
'filter[landing_page][null]' => '',
'filter[associate_with_landing_page][eq]' => '1'
)
);
$associated = $api->getData();

$api2 = $this->getApi();
$api2->setEndpoint('stubs_and_pages');
$api2->setArguments(
array(
'fields' => '*.*.*.*',
'meta' => 'result_count,total_count,type',
'filter[section][eq]' => 'visit-us',
'filter[landing_page][eq]' => '1',
)
);
$pages = $api2->getData();
$associated = Stubs::visitUsAssociated();
$pages = Stubs::visitUsLanding();
$directions = Directions::list();
$floors = FloorPlans::list();
$corona = CoronaVirusNotes::list();
Expand Down
63 changes: 31 additions & 32 deletions app/Models/AffiliateResearchers.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,23 @@

class AffiliateResearchers extends Model
{
protected static string $table = 'affiliate_researchers';

/**
* Return a list of affiliate researchers
* @returns array
*/
public static function list(): array
{
$api = new DirectUs;
$api->setEndpoint('affiliate_researchers');
$api->setArguments(
array(
'fields' => '*.*.*.*',
'meta' => 'result_count,total_count,type',
'sort' => 'last_name'
)
);
return $api->getData();
$api = new DirectUs(
self::$table,
array(
'fields' => '*.*.*.*',
'meta' => 'result_count,total_count,type',
'sort' => 'last_name'
)
);
return $api->getData();
}

/**
Expand All @@ -30,32 +31,30 @@ public static function list(): array
*/
public static function find(string $slug): array
{
$api = new DirectUs;
$api->setEndpoint('affiliate_researchers');
$api->setArguments(
array(
'fields' => '*.*.*.*',
'meta' => '*',
'filter[slug][eq]' => $slug
)
);
return $api->getData();
$api = new DirectUs(
self::$table,
array(
'fields' => '*.*.*.*',
'meta' => '*',
'filter[slug][eq]' => $slug
)
);
return $api->getData();
}

/**
* Return a list of affiliate researchers for a department
* @param int $department The department ID number
* @returns array
* @param int $department
* @return array
*/
public static function findByDepartment(int $department): array
{
$api = new DirectUs;
$api->setEndpoint('affiliate_researchers');
$api->setArguments(
array(
'fields' => '*.*.*.*',
'filter[departments_affiliated.department][in]' => $department,
)
);
return $api->getData();
$api = new DirectUs(
self::$table,
array(
'fields' => '*.*.*.*',
'filter[departments_affiliated.department][in]' => $department,
)
);
return $api->getData();
}
}
11 changes: 5 additions & 6 deletions app/Models/AssociatedPeople.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@

class AssociatedPeople extends Model
{
protected static string $table = 'associated_people';
/**
* @param string $filters
* @return array
*/
public static function list(string $filters = 'podcastGuest'): array
{
$api = new DirectUs;
$api->setEndpoint('associated_people');
$api->setArguments(
$api = new DirectUs(
self::$table,
array(
'fields' => '*.*.*.*',
'sort' => 'surname',
Expand All @@ -30,9 +30,8 @@ public static function list(string $filters = 'podcastGuest'): array
*/
public static function find(string $slug): array
{
$api = new DirectUs;
$api->setEndpoint('associated_people');
$api->setArguments(
$api = new DirectUs(
self::$table,
array(
'fields' => '*.*.*.*.*',
'meta' => '*',
Expand Down
Loading