Source

TrafficLight / src / Core / Services / ProjectService.cs

Full commit
using System.Collections.Generic;
using RossCode.TrafficLight.Core.Domain;
using RossCode.TrafficLight.Core.Eventing;
using RossCode.TrafficLight.Core.Eventing.Events;

namespace RossCode.TrafficLight.Core.Services
{
    public class ProjectService : IProjectService
    {
        private readonly IProjectStatusResolverService projectStatusResolverService;
        private BuildStatus existingBuildStatus = BuildStatus.Unknown;

        public ProjectService(IProjectStatusResolverService projectStatusResolverService)
        {
            this.projectStatusResolverService = projectStatusResolverService;
        }

        public void CheckAndSetStatusesFor(IList<Project> projects)
        {
            foreach(var project in projects)
            {
                project.CurrentStatus = projectStatusResolverService.CheckStatus(project);
            }

            var buildStatus = BuildStatus.Unknown;

            foreach (var project in projects)
            {
                switch (buildStatus)
                {
                    case BuildStatus.Unknown:
                    case BuildStatus.Success: 
                        buildStatus = project.CurrentStatus;
                        break;
                    case BuildStatus.Failing:
                        if (project.CurrentStatus == BuildStatus.Building)
                        {
                            buildStatus = project.CurrentStatus;
                        }
                        break;
                }

            }

            if (existingBuildStatus == buildStatus) return;

            existingBuildStatus = buildStatus;
            DomainEvents.Raise(new BuildStatusChanged(buildStatus));
        }
    }
}