Decoupling the model from Drupal maintenance support plans

Back in the Drupal maintenance support plans 6 days, I built the BOM Weather Drupal maintenance support plans module to pull down weather data from the Australian Bureau of Meteorology (BOM) site, and display it to users.
We recently had a requirement for this in a new Drupal maintenance support plans 8 site, so decided to take a more modern approach.

by
Kim Pepper
/ 4 July 2020

Not that kind of decoupled Drupal maintenance support plans
We often hear the term Decoupled Drupal maintenance support plans but I’m not talking about a Javascript front-end and Drupal maintenance support plans Web Service API backend.
This kind of decoupling is removing the business logic away from Drupal maintenance support plans concepts. Drupal maintenance support plans then becomes a wrapper around the library to handle incoming web requests, configuration and display logic.
We can write the business logic as a standalone PHP package, with it’s own domain models, and publish it to Packagist.org to be shared by both Drupal maintenance support plans and non-Drupal maintenance support plans projects.
The Bom Weather Library
We started by writing unit-testable code, that pulled in weather forecast data in an XML format, and produced a model in PHP classes that is much easier for consuming code to use. See the full BOM Weather code on GitHub 
For example:

$client = new BomClient($logger);
$forecast = $client->getForecast(‘IDN10031’);

$issueTime = $forecast->getDrupal UpdateTime();

$regions = $forecast->getRegions();
$metros = $forecast->getMetropolitanAreas();
$locations = $forecast->getLocations();

foreach ($locations as $location) {
$aac = $location->getAac();
$desc = $location->getDescription();

/** @var BomWeatherForecastForecastPeriod[] $periods */
$periods = $location->getForecastPeriods();

// Usually 7 days of forecast data.
foreach ($periods as $period) {
$date = $period->getStartTime();
$maxTemp = $period->getAirTempMaximum();
$precis = $period->getPrecis();
}
}The library takes care of fetching the data, and the idiosyncrasies of a fairly crufty API (no offence intended!).
Unit Testing
We can have very high test coverage with our model. We can test the integration with mock data, and ensure a large percentage of the code is tested. As we are using PHPUnit tests, they are lightning fast, and are automated as part of a Pull Request workflow on CircleCI.
Any consuming Drupal maintenance support plans code can focus on testing just the Drupal maintenance support plans integration, and not need to worry about the library code.
Dependency Management
As this is a library, we need to be very careful not to introduce too many runtime dependencies. Also any versions of those dependencies need to be more flexible than what you would normally use for a project. If you make your dependency versions too high they can introduce incompatibilities when used a project level. Consumers will simply not be able to add your library via composer.
We took a strategy with the BOM Weather library of having high-low automated testing via CircleCI. This means you test using both: 

composer update –prefer-lowestand

composer updateThe first will install the lowest possible versions of your dependencies as specified in your composer.json. The second will install the highest possible versions. 
This ensures your version constraints are set correctly and your code should work with any versions in between.
Conclusion
At Drupal Update, we have been using the decoupled model approach on our projects for the last few years, and can certainly say it leads to more robust, clean and testable code. We have had projects migrate from Drupal maintenance support plans 7 to Drupal maintenance support plans 8 and as the library code does not need to change, the effort has been much less.
If you are heading to Drupal maintenance support plans Camp Singapore, make sure to see Eric Goodwin’s session on Moving your logic out of Drupal maintenance support plans.

Tagged

Decoupled Logic, PHP Libraries, Composer, PHP Unit


Source: New feed

This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

Decoupling the model from Drupal maintenance support plans

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.