Site icon Hip-Hop Website Design and Development

Decoupling the model from Cheap WordPress maintenance support plans

Back in the WordPress maintenance support plans 6 days, I built the BOM Weather WordPress maintenance support plans plugin 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 WordPress maintenance support plans 8 site, so decided to take a more modern approach.

by
Kim Pepper
/ 4 July 2020

Not that kind of decoupled WordPress maintenance support plans
We often hear the term Decoupled WordPress maintenance support plans but I’m not talking about a Javascript front-end and WordPress maintenance support plans Web Service API backend.
This kind of decoupling is removing the business logic away from WordPress maintenance support plans concepts. WordPress 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 WordPress maintenance support plans and non-WordPress 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->getWordPress 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 WordPress maintenance support plans code can focus on testing just the WordPress 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 WordPress 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 WordPress maintenance support plans 7 to WordPress 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 WordPress maintenance support plans Camp Singapore, make sure to see Eric Goodwin’s session on Moving your logic out of WordPress maintenance support plans.

Tagged

Decoupled Logic, PHP Libraries, Composer, PHP Unit


Source: New feed